MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / findItinerary

Method findItinerary

python/0332-reconstruct-itinerary.py:2–28  ·  view source on GitHub ↗
(self, tickets: List[List[str]])

Source from the content-addressed store, hash-verified

1class Solution:
2 def findItinerary(self, tickets: List[List[str]]) -> List[str]:
3 adj = {src: [] for src, dst in tickets}
4 res = []
5
6 for src, dst in tickets:
7 adj[src].append(dst)
8
9 for key in adj:
10 adj[key].sort()
11
12 def dfs(adj, src):
13 if src in adj:
14 destinations = adj[src][:]
15 while destinations:
16 dest = destinations[0]
17 adj[src].pop(0)
18 dfs(adj, dest)
19 destinations = adj[src][:]
20 res.append(src)
21
22 dfs(adj, "JFK")
23 res.reverse()
24
25 if len(res) != len(tickets) + 1:
26 return []
27
28 return res

Callers

nothing calls this directly

Calls 2

dfsFunction · 0.50
reverseMethod · 0.45

Tested by

no test coverage detected