-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
35 lines (33 loc) · 1022 Bytes
/
Copy pathsolution.java
File metadata and controls
35 lines (33 loc) · 1022 Bytes
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
// 332. Reconstruct Itinerary
// https://leetcode.com/problems/reconstruct-itinerary/
// Hard | Java | Accepted 2026-01-07
// Runtime 5 ms | Memory 47.3 MB
class Solution {
Map<String, PriorityQueue<String>> map = new HashMap<>();
List<String> ans = new ArrayList<>();
public List<String> findItinerary(List<List<String>> tickets) {
for(int i = 0; i<tickets.size(); i++)
{
List<String> temp = tickets.get(i);
if(!map.containsKey(temp.get(0)))
{
map.put(temp.get(0), new PriorityQueue<>());
}
map.get(temp.get(0)).add(temp.get(1));
}
List<String> b = new ArrayList<>();
b.add("JFK");
recurse("JFK");
return ans.reversed();
}
public void recurse(String node)
{
PriorityQueue<String> t = map.get(node);
while(t!=null && !t.isEmpty())
{
String temp = t.poll();
recurse(temp);
}
ans.add(node);
}
}