Reconstruct Itinerary
LeetCode Problem #332 (Medium)
Problem Statement
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to]
, reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK
. Thus, the itinerary must begin with JFK
.
Testcases
Prerequisite Knowledge
Depth-First-Search
Refer to the Prerequisite Knowledge section in Surrounded Regions:
Priority Queue
Refer to the Prerequisite Knowledge section in Cheapest Flights Within K Stops:
Min-Heap
In its array representation, the index of the left child is given by 2i + 1
and the index of the right child is given by 2i + 2
, where i
is the current element.
Problem Explanation
In this problem, we are required to construct a sequential itinerary based on the given list of airline tickets which each contain a [from, to]
pair of three-letter airport names.
The traveller begins their journey at the "JFK"
airport, so our itinerary must always begin with "JFK"
. The subsequent destinations are mapped out according to their sequential occurrence.
Example 1
Consider the following airline tickets to be the input:
[["MUC", "LHR"], ["JFK", MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
The adjacency list for the given input could be represented as:
"JFK" -> "MUC"
"MUC" -> "LHR"
"LHR" -> "SFO"
"SFO" -> "SJC"
Therefore, the resultant itinerary would be:
"JFK" -> "MUC" -> "LHR" -> "SFO" -> "SJC"
In this example, there are no conflicts in terms of the sequential arrangement, which may arise if one airport is visited twice, thus creating more than one valid itinerary.
Example 2
In case more than one valid itinerary can be generated, the problem stipulates that we must return the itinerary which has the smallest lexical order.
We can understand this by considering the following airline tickets to be the input:
[["JFK", "SFO"], ["JFK", ATL"], ["SFO", "ATL"], ["ATL", "JFK"], ["ATL", "SFO"]]
One adjacency list for the given input could be represented as:
"JFK" -> "SFO"
"SFO" -> "ATL"
"ATL" -> "JFK"
"JFK" -> "ATL"
"ATL" -> "SFO"
Therefore, the resultant itinerary would be:
"JFK" -> "SFO" -> "ATL" -> "JFK" -> "ATL" -> "SFO"
Another adjacency list for the given input could be represented as:
"JFK" -> "ATL"
"ATL" -> "JFK"
"JFK" -> "SFO"
"SFO" -> "ATL"
"ATL" -> "SFO"
Therefore, the resultant itinerary would be:
"JFK" -> "SFO" -> "ATL" -> "JFK" -> "ATL" -> "SFO"
We would return the latter because the first point of difference which occurs in both lists, is the choice of destinations from "JFK"
at the beginning. The two options are "ATL"
and "SFO"
. Alphabetically, "ATL"
occurs before "SFO"
, so this is the option that we process.
Algorithm
Depth-First-Search Approach
In this approach, we use DFS to travel to each destination from a given starting point. This in effect, creates an adjacency matrix of[from, to]
values from the given information. To ensure that the airport names are arranged in the correct lexicographical order, we use a min-heap structure.
Declare a global map variable which contains a string and priority queue as the key-value pair to store the information about the airports.
Declare a global string vector variable to store the resultant itinerary.
Find Itinerary Function
Using a min-heap, store the names of the airports in lexicographical order.
Begin the DFS process from "JFK" since that is the starting point.
DFS Helper Function
Pop the airports from the priority queue one-by-one.
When each new airport is popped, perform DFS.
When the priority queue is empty, add the airport names to the resultant vector and return it.
Code
Last updated
Was this helpful?