#332
Reconstruct Itinerary
hard· Advanced Graphsruns: 0You are given a list of airline tickets where tickets[i] = [from, to] represents a one-way flight. Reconstruct the itinerary in order. All tickets belong to a traveler who departs from 'JFK', so the itinerary must begin with 'JFK'. If there are multiple valid itineraries, return the itinerary with the smallest lexical order when read as a single string.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 452
from collections import defaultdict
class Solution:
def findItinerary(self, tickets: list[list[str]]) -> list[str]:
graph = defaultdict(list)
for src, dst in sorted(tickets, reverse=True):
graph[src].append(dst)
result = []
def dfs(node: str) -> None:
while graph[node]:
dfs(graph[node].pop())
result.append(node)
dfs("JFK")
return result[::-1]
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.