Skip to content

Commit 92bb311

Browse files
authored
Create 332-Reconstruct-Itinerary.py
1 parent 4b69f87 commit 92bb311

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

332-Reconstruct-Itinerary.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
3+
adj = { u:collections.deque() for u, v in tickets }
4+
res = ["JFK"]
5+
6+
tickets.sort()
7+
for u, v in tickets:
8+
adj[u].append(v)
9+
10+
def dfs(cur):
11+
if len(res) == len(tickets) + 1:
12+
return True
13+
if cur not in adj:
14+
return False
15+
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
25+
dfs("JFK")
26+
return res

0 commit comments

Comments
 (0)