Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a181b9

Browse files
solves #332: Reconstruct Itinerary in java
1 parent 398cdea commit 0a181b9

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
| 328 | [Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list) | | |
278278
| 331 | [Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree) | | |
279279
| 331 | 🔒 [Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree) | | |
280+
| 332 | [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary) | [![Java](assets/java.png)](src/ReconstructItinerary.java) | |
280281
| 334 | [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence) | | |
281282
| 337 | [House Robber III](https://leetcode.com/problems/increasing-triplet-subsequence) | | |
282283
| 338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | [![Java](assets/java.png)](src/CountingBits.java) [![Python](assets/python.png)](python/counting_bits.py) | |

‎src/ReconstructItinerary.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// https://leetcode.com/problems/reconstruct-itinerary
2+
// E = |flights|, V = |airports| N = E / V
3+
// T: O(V * NlogN) = O(E log(E / V))
4+
// S: O(V + E)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.PriorityQueue;
11+
import java.util.Queue;
12+
13+
public class ReconstructItinerary {
14+
public List<String> findItinerary(List<List<String>> tickets) {
15+
final Map<String, Queue<String>> graph = createGraph(tickets);
16+
final List<String> result = new ArrayList<>();
17+
dfs(graph, "JFK", result);
18+
return result.reversed();
19+
}
20+
21+
private static void dfs(Map<String, Queue<String>> graph, String current, List<String> result) {
22+
final Queue<String> queue = graph.getOrDefault(current, new PriorityQueue<>());
23+
while (!queue.isEmpty()) {
24+
final String to = queue.poll();
25+
dfs(graph, to, result);
26+
}
27+
result.add(current);
28+
}
29+
30+
private static Map<String, Queue<String>> createGraph(List<List<String>> tickets) {
31+
final Map<String, Queue<String>> graph = new HashMap<>();
32+
for (List<String> ticket : tickets) {
33+
final String from = ticket.get(0), to = ticket.get(1);
34+
final Queue<String> queue = graph.getOrDefault(from, new PriorityQueue<>());
35+
queue.add(to);
36+
graph.putIfAbsent(from, queue);
37+
}
38+
return graph;
39+
}
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /