Skip to content

Commit ac96eda

Browse files
committed
feat: 332. Reconstruct Itinerary : 欧拉路径
1 parent 86f8625 commit ac96eda

File tree

5 files changed

+1799
-2123
lines changed

5 files changed

+1799
-2123
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
-> D -> E
3+
A -> B <-> F
4+
<-> G
5+
[["JFK","BBB"],["BBB","FFF"],["FFF","BBB"],["BBB","GGG"],["GGG","BBB"],["BBB","DDD"],["DDD","EEE"]]
6+
*/
7+
function findItinerary (tickets: string[][]): string[] {
8+
const e: Record<string, string[]> = {}
9+
for (const [f, t] of tickets) {
10+
e[f] ??= [], e[f].push(t)
11+
}
12+
for (const v of Object.values(e)) v.sort((a, b) => a < b ? 1 : -1)
13+
console.log(e)
14+
const dfs = (x: string) => {
15+
const paths: string[][] = []
16+
// e: {BBB: [ 'GGG', 'FFF', 'DDD' ],}
17+
// 最多循环两次,第一次是 DE,第二次是 FBGB
18+
while (e[x]?.length) {
19+
// 走一个字典序最小的 y
20+
const y = e[x].pop()!
21+
paths.push(dfs(y))
22+
}
23+
const ans = [x]
24+
console.log(x, paths)
25+
while (paths.length) {
26+
const p = paths.pop()!
27+
ans.push(...p)
28+
}
29+
return ans
30+
}
31+
return dfs('JFK')
32+
};

leetcode/残酷刷题/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [表达式求值](#表达式求值)
2727
- [状态机](#状态机)
2828
- [链表](#链表)
29+
- [欧拉路径](#欧拉路径)
2930
- [Links](#links)
3031

3132
## Problems
@@ -179,6 +180,10 @@
179180
- 2074. Reverse Nodes in Even Length Groups
180181
- 143. Reorder List
181182

183+
### 欧拉路径
184+
185+
- 332. Reconstruct Itinerary
186+
182187
## Links
183188

184189
- https://wisdompeak.github.io/lc-score-board/

0 commit comments

Comments
 (0)