๐๏ธ Algorithm/โฌ ํ๋ก๊ทธ๋๋จธ์ค
โฌ [Programmers] [Python] [Level3] ์ฌํ ๊ฒฝ๋ก
Dbswnstjd
2023. 5. 10. 11:36
๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/43164/solution_groups?language=python3
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
ํ์ด
# ํ๋ก๊ทธ๋๋จธ์ค 3๋จ๊ณ - ์ฌํ ๊ฒฝ๋ก
from collections import defaultdict
def solution(tickets):
path = []
graph = defaultdict(list)
for (start, end) in tickets:
graph[start].append(end)
for airport in graph.keys():
graph[airport].sort(reverse=True)
stack = ['ICN']
while stack:
top = stack.pop()
if top not in graph or not graph[top]:
path.append(top)
else:
stack.append(top)
stack.append(graph[top].pop())
return path[::-1]
DFS ๋ฅผ ํตํด ํ ์ ์๋ ๋ฌธ์ ์ด๋ค.