-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL332.java
More file actions
61 lines (57 loc) · 2.28 KB
/
L332.java
File metadata and controls
61 lines (57 loc) · 2.28 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
class Solution332 {
class Solution {
private int totalSize;
/**
* 332. Reconstruct Itinerary https://leetcode.com/problems/reconstruct-itinerary/description/
*
* @param tickets String[][]
* List of all tickets.
* @return Possible itinerary
* @timeComplexity O(n ^ 2) where n is the number of airports For each source, all possible destinations will be tried out in worst case.
* @spaceComplexity O(n) Keep current itinerary and priority queue of destinations from each source
*/
public List<String> findItinerary(String[][] tickets) {
totalSize = tickets.length;
Map<String, PriorityQueue<String>> map = new HashMap<>();
for (int i = 0; i < tickets.length; i++) {
if (map.get(tickets[i][0]) == null) {
map.put(tickets[i][0], new PriorityQueue<>(Arrays.asList(tickets[i][1])));
} else {
map.get(tickets[i][0]).add(tickets[i][1]);
}
}
List<String> itin = new ArrayList<>();
solve(itin, "JFK", map);
return itin;
}
void solve(List<String> itin, String curSource, Map<String, PriorityQueue<String>> map) {
itin.add(curSource);
// Check whether we used up all the tickets
if (itin.size() == totalSize + 1) {
return;
}
PriorityQueue<String> destinations = map.get(curSource);
if (destinations != null) {
while (!destinations.isEmpty()) {
// Try this arrival airport
String dest = destinations.poll();
int curItinLength = itin.size();
solve(itin, dest, map);
// Did it work?
if (itin.size() < totalSize + 1) {
// If no then backtrack
itin = itin.subList(0, curItinLength);
} else {
return;
}
}
}
}
}
}