-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path43164.py
More file actions
33 lines (24 loc) · 818 Bytes
/
43164.py
File metadata and controls
33 lines (24 loc) · 818 Bytes
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
# https://programmers.co.kr/learn/courses/30/lessons/43164?language=python3
def DFS(graph, root):
visited = []
stack = [root]
while stack:
current = stack[-1]
if current not in graph or len(graph[current]) == 0:
visited.append(stack.pop())
else:
stack.append(graph[current].pop())
return visited
def solution(tickets):
cities = set()
for ticket in tickets:
cities |= set(ticket)
graph = {city: [] for city in cities}
for ticket in tickets:
graph[ticket[0]].append(ticket[1])
for key, value in graph.items():
value.sort(reverse=True)
answer = DFS(graph, 'ICN')
answer.reverse()
return answer
print(solution([["ICN", "SFO"], ["ICN", "ATL"], ["SFO", "ATL"], ["ATL", "ICN"], ["ATL", "SFO"]]))