/ ALGORITHM

프로그래머스 - 여행경로

image-20230118210931272

from collections import defaultdict
def solution(tickets):
    airports = defaultdict(list)
    for x,y in tickets:
        airports[x].append(y)
        
    for i in airports.values():
        i.sort()

    stack = ["ICN"]
    path = []

    while stack:
        tail = stack[-1]

        if airports[tail]:
            stack.append(airports[tail].pop(0))

        else:
            path.append(stack.pop())

    return path[::-1]