-
Notifications
You must be signed in to change notification settings - Fork 69
/
ReconstructItinerary.java
40 lines (36 loc) Β· 1.02 KB
/
ReconstructItinerary.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public List<String> findItinerary(List<List<String>> tickets) {
LinkedList<String> ans = new LinkedList<>();
Map<String, Queue<String>> graph = new HashMap<>();
for (final List<String> ticket : tickets) {
graph.putIfAbsent(ticket.get(0), new PriorityQueue<>());
graph.get(ticket.get(0)).offer(ticket.get(1));
}
dfs(graph, "JFK", ans);
return ans;
}
private void dfs(Map<String, Queue<String>> graph, final String u, LinkedList<String> ans) {
final Queue<String> arrivals = graph.get(u);
while (arrivals != null && !arrivals.isEmpty())
dfs(graph, arrivals.poll(), ans);
ans.addFirst(u);
}
}
/***
Test Case 1 :
Input
tickets =
[["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
Output
["JFK","MUC","LHR","SFO","SJC"]
Expected
["JFK","MUC","LHR","SFO","SJC"]
Test Case 2 :
Input
tickets =
[["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output
["JFK","ATL","JFK","SFO","ATL","SFO"]
Expected
["JFK","ATL","JFK","SFO","ATL","SFO"]
*/