forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_332.java
35 lines (31 loc) · 1.14 KB
/
_332.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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
public class _332 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/36383/share-my-solution
*/
public List<String> findItinerary(String[][] tickets) {
Map<String, PriorityQueue<String>> flights = new HashMap<>();
LinkedList<String> path = new LinkedList<>();
for (String[] ticket : tickets) {
flights.putIfAbsent(ticket[0], new PriorityQueue<>());
flights.get(ticket[0]).add(ticket[1]);
}
dfs("JFK", flights, path);
return path;
}
public void dfs(String departure, Map<String, PriorityQueue<String>> flights,
LinkedList path) {
PriorityQueue<String> arrivals = flights.get(departure);
while (arrivals != null && !arrivals.isEmpty()) {
dfs(arrivals.poll(), flights, path);
}
path.addFirst(departure);
}
}
}