[Leetcode]332. Reconstruct Itinerary

김지원·2022년 5월 5일
0
post-custom-banner
class Solution:
    def findItinerary(self, tickets: List[List[str]]) -> List[str]:
        
        _dict=defaultdict(list)
        
        for ticket in sorted(tickets, reverse=True):
            _dict[ticket[0]].append(ticket[1])
        
        plan=[]
        
        def dfs(start):            
            while _dict[start]:
                dest=_dict[start].pop()
                dfs(dest)
            plan.append(start)

        dfs("JFK")        
        return plan[::-1]

References

profile
Make your lives Extraordinary!
post-custom-banner

0개의 댓글