File tree Expand file tree Collapse file tree 1 file changed +21
-20
lines changed Expand file tree Collapse file tree 1 file changed +21
-20
lines changed Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def findItinerary (self , tickets : List [List [str ]]) -> List [str ]:
3
- adj = {u : collections . deque () for u , v in tickets }
4
- res = ["JFK" ]
3
+ adj = {src : [] for src , dst in tickets }
4
+ res = []
5
5
6
- tickets .sort ()
7
- for u , v in tickets :
8
- adj [u ].append (v )
6
+ for src , dst in tickets :
7
+ adj [src ].append (dst )
9
8
10
- def dfs (cur ):
11
- if len (res ) == len (tickets ) + 1 :
12
- return True
13
- if cur not in adj :
14
- return False
9
+ for key in adj :
10
+ adj [key ].sort ()
15
11
16
- temp = list (adj [cur ])
17
- for v in temp :
18
- adj [cur ].popleft ()
19
- res .append (v )
20
- if dfs (v ):
21
- return res
22
- res .pop ()
23
- adj [cur ].append (v )
24
- return False
12
+ def dfs (adj , result , src ):
13
+ if src in adj :
14
+ destinations = adj [src ][:]
15
+ while destinations :
16
+ dest = destinations [0 ]
17
+ adj [src ].pop (0 )
18
+ dfs (adj , res , dest )
19
+ destinations = adj [src ][:]
20
+ res .append (src )
21
+
22
+ dfs (adj , res , "JFK" )
23
+ res .reverse ()
24
+
25
+ if len (res ) != len (tickets ) + 1 :
26
+ return []
25
27
26
- dfs ("JFK" )
27
28
return res
You can’t perform that action at this time.
0 commit comments