1
1
package com .fishercoder .solutions ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .Collections ;
5
- import java .util .Comparator ;
6
3
import java .util .HashMap ;
7
4
import java .util .LinkedList ;
8
5
import java .util .List ;
9
6
import java .util .Map ;
10
7
import java .util .PriorityQueue ;
11
8
12
- /**
13
- * 332. Reconstruct Itinerary
14
- *
15
- * Given a list of airline tickets represented by pairs of departure and arrival airports [from, to],
16
- * reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.
17
-
18
- Note:
19
- If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.
20
- For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
21
-
22
- All airports are represented by three capital letters (IATA code).
23
-
24
- You may assume all tickets form at least one valid itinerary.
25
- Example 1:
26
- tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
27
- Return ["JFK", "MUC", "LHR", "SFO", "SJC"].
28
-
29
- Example 2:
30
- tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
31
- Return ["JFK","ATL","JFK","SFO","ATL","SFO"].
32
- Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order.
33
- */
34
9
public class _332 {
35
10
36
11
public static class Solution1 {
37
- /** credit: https://discuss.leetcode.com/topic/36383/share-my-solution */
12
+ /**
13
+ * credit: https://discuss.leetcode.com/topic/36383/share-my-solution
14
+ */
38
15
public List <String > findItinerary (String [][] tickets ) {
39
16
Map <String , PriorityQueue <String >> flights = new HashMap <>();
40
17
LinkedList <String > path = new LinkedList <>();
@@ -47,7 +24,7 @@ public List<String> findItinerary(String[][] tickets) {
47
24
}
48
25
49
26
public void dfs (String departure , Map <String , PriorityQueue <String >> flights ,
50
- LinkedList path ) {
27
+ LinkedList path ) {
51
28
PriorityQueue <String > arrivals = flights .get (departure );
52
29
while (arrivals != null && !arrivals .isEmpty ()) {
53
30
dfs (arrivals .poll (), flights , path );
0 commit comments