-
Notifications
You must be signed in to change notification settings - Fork 64
/
sol.java
32 lines (25 loc) · 1019 Bytes
/
sol.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
import java.util.*;
public class sol {
public static void main(String[] args) {
// call the fn here
}
class Solution {
public List<String> findItinerary(List<List<String>> tickets) {
Map<String, PriorityQueue<String>> graph = new HashMap<>();
for (List<String> ticket : tickets) {
graph.putIfAbsent(ticket.get(0), new PriorityQueue<>());
graph.get(ticket.get(0)).add(ticket.get(1));
}
LinkedList<String> itinerary = new LinkedList<>();
dfs("JFK", graph, itinerary);
return itinerary;
}
private void dfs(String airport, Map<String, PriorityQueue<String>> graph, LinkedList<String> itinerary) {
PriorityQueue<String> nextAirports = graph.get(airport);
while (nextAirports != null && !nextAirports.isEmpty()) {
dfs(nextAirports.poll(), graph, itinerary);
}
itinerary.addFirst(airport);
}
}
}