-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path332.reconstruct-itinerary.java
More file actions
78 lines (71 loc) · 2.29 KB
/
Copy path332.reconstruct-itinerary.java
File metadata and controls
78 lines (71 loc) · 2.29 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* @lc app=leetcode id=332 lang=java
*
* [332] Reconstruct Itinerary
*
* https://leetcode.com/problems/reconstruct-itinerary/description/
*
* algorithms
* Medium (31.56%)
* Total Accepted: 80.4K
* Total Submissions: 254.6K
* Testcase Example: '[["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]'
*
* 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:
*
*
* Input: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
* Output: ["JFK", "MUC", "LHR", "SFO", "SJC"]
*
*
* Example 2:
*
*
* Input:
* [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
* Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
* Explanation: Another possible reconstruction is
* ["JFK","SFO","ATL","JFK","ATL","SFO"].
* But it is larger in lexical order.
*
*
*/
class Solution {
public List<String> findItinerary(List<List<String>> tickets) {
LinkedList<String> itinerary = new LinkedList<>();
if (tickets.size() == 0) {
return itinerary;
}
Map<String, PriorityQueue<String>> map = new HashMap<>();
for (List<String> ticket : tickets) {
map.putIfAbsent(ticket.get(0), new PriorityQueue<>());
map.get(ticket.get(0)).offer(ticket.get(1));
}
// Try DFS and find out the correct path
findPath(map, "JFK", itinerary);
return itinerary;
}
private void findPath(Map<String, PriorityQueue<String>> map, String cur, LinkedList<String> itinerary) {
PriorityQueue<String> destinations = map.get(cur);
while (destinations != null && !destinations.isEmpty()) {
findPath(map, destinations.poll(), itinerary);
}
itinerary.addFirst(cur);
}
}