forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconstruct-itinerary.py
39 lines (32 loc) · 1.21 KB
/
reconstruct-itinerary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Time: O(t! / (n1! * n2! * ... nk!)), t is the total number of tickets,
# ni is the number of the ticket which from is city i,
# k is the total number of cities.
# Space: O(t)
import collections
class Solution(object):
def findItinerary(self, tickets):
"""
:type tickets: List[List[str]]
:rtype: List[str]
"""
def route_helper(origin, ticket_cnt, graph, ans):
if ticket_cnt == 0:
return True
for i, (dest, valid) in enumerate(graph[origin]):
if valid:
graph[origin][i][1] = False
ans.append(dest)
if route_helper(dest, ticket_cnt - 1, graph, ans):
return ans
ans.pop()
graph[origin][i][1] = True
return False
graph = collections.defaultdict(list)
for ticket in tickets:
graph[ticket[0]].append([ticket[1], True])
for k in graph.keys():
graph[k].sort()
origin = "JFK"
ans = [origin]
route_helper(origin, len(tickets), graph, ans)
return ans