Published:
Updated:

  • Reference
  • graph
    • ๋ฐฑ์ค€: List
    • LeetCode: defaultdict(list)


Solution

import collections
from typing import List


class Solution:
    def findItinerary(self, tickets: List[List[str]]) -> List[str]:
        answer = []
        graph = collections.defaultdict(list)

        for x, y in sorted(tickets):
            graph[x].append(y)

        def dfs(x: str):
            # ์žฌ๊ท€๋กœ ๋‹ค ๋ฐฉ๋ฌธํ•œ๋‹ค๋Š” ๋œป
            while graph[x]:
                # ์–ดํœ˜ ์ˆœ ๋ฐฉ๋ฌธ -> ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ๊ฐ’
                dfs(graph[x].pop(0))

            answer.append(x)

        dfs('JFK')

        answer.reverse()
        return answer

Leave a comment