-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconstruct_itinerary.py
More file actions
26 lines (22 loc) · 938 Bytes
/
reconstruct_itinerary.py
File metadata and controls
26 lines (22 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import heapq
from collections import defaultdict, deque
from typing import List
class ReconstructItinerary:
@staticmethod
def findItinerary(tickets: List[List[str]]) -> List[str]:
# Since we need to return the path in lexical order, we will
# use minHeap to store airports. Thus, we need a map that will
# connect the departure and arrival airports
flights = defaultdict(list)
# List to store the final path
path = deque()
# Process all flights to create the mappings
for departure, arrival in tickets:
heapq.heappush(flights[departure], arrival)
def dfs(departure_airport):
while flights[departure_airport]:
dfs(heapq.heappop(flights[departure_airport]))
path.appendleft(departure_airport)
# We start from JFK, hence we will perform DFS from there
dfs("JFK")
return list(path)