Skip to content

Commit 1f8789e

Browse files
committed
Create ReconstructItinerary.java
1 parent 014cc3e commit 1f8789e

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

ReconstructItinerary.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package leetcode;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Project Name : Leetcode
7+
* Package Name : leetcode
8+
* File Name : ReconstructItinerary
9+
* Creator : Leo
10+
* Description : 332. Reconstruct Itinerary
11+
*/
12+
public class ReconstructItinerary {
13+
14+
/**
15+
* Given a list of airline tickets represented by pairs of departure and arrival airports [from, to],
16+
* reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK.
17+
* Thus, the itinerary must begin with JFK.
18+
19+
Note:
20+
If there are multiple valid itineraries, you should return the itinerary that
21+
has the smallest lexical order when read as a single string. For example,
22+
the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
23+
All airports are represented by three capital letters (IATA code).
24+
You may assume all tickets form at least one valid itinerary.
25+
26+
Example 1:
27+
tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
28+
Return ["JFK", "MUC", "LHR", "SFO", "SJC"].
29+
30+
Example 2:
31+
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
32+
Return ["JFK","ATL","JFK","SFO","ATL","SFO"].
33+
34+
Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"].
35+
But it is larger in lexical order.
36+
37+
38+
DFS(HashMap) + PriorityQueue
39+
40+
JFK SFO
41+
|
42+
ATL
43+
44+
45+
time : O(nlogn)
46+
space : O(n)
47+
48+
*/
49+
50+
HashMap<String, PriorityQueue<String>> map;
51+
List<String> res;
52+
53+
public List<String> findItinerary(String[][] tickets) {
54+
map = new HashMap<>();
55+
res = new LinkedList<>();
56+
for (String[] ticket : tickets) {
57+
map.computeIfAbsent(ticket[0], k -> new PriorityQueue<>()).add(ticket[1]);
58+
}
59+
helper("JFK");
60+
return res;
61+
}
62+
63+
private void helper(String airport) {
64+
while (map.containsKey(airport) && !map.get(airport).isEmpty()) {
65+
helper(map.get(airport).poll());
66+
}
67+
res.add(0, airport);
68+
}
69+
70+
public List<String> findItinerary2(String[][] tickets) {
71+
HashMap<String, PriorityQueue<String>> map = new HashMap<>();
72+
73+
for (String[] ticket : tickets) {
74+
map.computeIfAbsent(ticket[0], k -> new PriorityQueue()).add(ticket[1]);
75+
}
76+
77+
List<String> res = new LinkedList();
78+
Stack<String> stack = new Stack<>();
79+
stack.push("JFK");
80+
81+
while (!stack.empty()) {
82+
while (map.containsKey(stack.peek()) && !map.get(stack.peek()).isEmpty()) {
83+
stack.push(map.get(stack.peek()).poll());
84+
}
85+
res.add(0, stack.pop());
86+
}
87+
88+
return res;
89+
}
90+
}

0 commit comments

Comments
 (0)