Reconstruct Itinerary
Problem
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
.
Note:
- If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
- All airports are represented by three capital letters (IATA code).
- You may assume all tickets form at least one valid itinerary.
Example 1:
tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Return ["JFK", "MUC", "LHR", "SFO", "SJC"].
Example 2:
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Return ["JFK","ATL","JFK","SFO","ATL","SFO"].
Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order.
Solution
Iterative solution using HashMap
public class Solution {
public List<String> findItinerary(String[][] tickets) {
if (tickets == null || tickets.length == 0) return new ArrayList<>();
List<String> res = new ArrayList<>();
Map<String, PriorityQueue<String>> map = new HashMap<>();
Stack<String> stack = new Stack<>();
for (String[] ticket : tickets) {
map.computeIfAbsent(ticket[0], k -> new PriorityQueue<>()).add(ticket[1]); //Cannot use putIfAbsent(), explained in Analysis
}
stack.push("JFK");
while (!stack.isEmpty()) {
while (map.containsKey(stack.peek()) && !map.get(stack.peek()).isEmpty()) {
stack.push(map.get(stack.peek()).poll());
}
res.add(0, stack.pop());
}
return res;
}
}
DFS recursion solution
public class Solution {
public List<String> findItinerary(String[][] tickets) {
if (tickets == null || tickets.length == 0) return new ArrayList<>();
List<String> res = new ArrayList<>();
Map<String, PriorityQueue<String>> map = new HashMap<>();
for (String[] ticket : tickets) {
map.computeIfAbsent(ticket[0], k -> new PriorityQueue<>()).add(ticket[1]); //Cannot use putIfAbsent(), explained in Analysis
}
dfs(res, map, "JFK");
return res;
}
private void dfs(List<String> res, Map<String, PriorityQueue<String>> map, String from) {
while (map.containsKey(from) && !map.get(from).isEmpty()) {
dfs(res, map, map.get(from).poll());
}
res.add(0, from);
}
}
Analysis
The idea of this solution is to use PriorityQueue<String>
to get arrival city in lexical order
We first iterate all ticket[]
from given tickets[][]
, and use map.computeIfAbsent()
to add arrival city
We cannot use putIfAbsent()
because if will return previous value
We must call computeIfAbsent()
, which will return the current value from the map
Then we use stack
or recursively add all cities as required