여행경로 - python (programmers)

참치돌고래·2021년 5월 18일
0

알고리즘

목록 보기
4/36
post-custom-banner

여행경로

-->https://programmers.co.kr/learn/courses/30/lessons/43164

def solution(tickets):
    answer = []
    routes = {}
    for t in tickets:
        routes[t[0]] = routes.get(t[0],[]) + [t[1]]
    for r in routes:
        routes[r].sort(reverse=True)
    start_airpot = ["ICN"]

    while start_airpot:
        start = start_airpot[-1]
        if start in routes and routes[start]:
            start_airpot.append(routes[start].pop())
            
        else:
            answer.append(start_airpot.pop())
    answer.reverse()
    return answer
profile
안녕하세요
post-custom-banner

0개의 댓글