Skip to content

Commit 919b5ba

Browse files
committed
332
1 parent 4e51d71 commit 919b5ba

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

332/Solution.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://discuss.leetcode.com/topic/36383/share-my-solution
2+
class Solution {
3+
4+
public List<String> findItinerary(String[][] tickets) {
5+
Map<String, PriorityQueue<String> > map = new HashMap<>();
6+
for (String[] pair : tickets) {
7+
PriorityQueue pq = map.getOrDefault(pair[0], new PriorityQueue<>());
8+
pq.offer(pair[1]);
9+
map.put(pair[0], pq);
10+
}
11+
LinkedList<String> ans = new LinkedList<>();
12+
dfs("JFK", map, ans);
13+
return ans;
14+
}
15+
16+
private void dfs (String start, Map<String, PriorityQueue<String> > map, LinkedList<String> ans) {
17+
PriorityQueue<String> pq = map.get(start);
18+
while (pq != null && !pq.isEmpty()) {
19+
dfs(pq.poll(), map, ans);
20+
}
21+
ans.addFirst(start);
22+
}
23+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
- [297 Serialize and Deserialize Binary Tree](./297)
132132
- [301 Remove Invalid Parentheses](./301)
133133
- [312 Burst Balloons](./312)
134+
- [332 Reconstruct Itinerary](./332)
134135
- [334 Increasing Triplet Subsequence](./334)
135136
- [337 House Robber III](./337)
136137
- [341 Flatten Nested List Iterator](./341)

0 commit comments

Comments
 (0)