@@ -33,75 +33,26 @@ All airports are represented by three capital letters (IATA code).
33
33
*/
34
34
public class _332 {
35
35
36
- /**credit: https://discuss.leetcode.com/topic/36383/share-my-solution*/
37
- public List <String > findItinerary (String [][] tickets ) {
38
- Map <String , PriorityQueue <String >> flights = new HashMap <>();
39
- LinkedList <String > path = new LinkedList <>();
40
- for (String [] ticket : tickets ) {
41
- flights .putIfAbsent (ticket [0 ], new PriorityQueue <>());
42
- flights .get (ticket [0 ]).add (ticket [1 ]);
43
- }
44
- dfs ("JFK" , flights , path );
45
- return path ;
46
- }
47
-
48
- public void dfs (String departure , Map <String , PriorityQueue <String >> flights , LinkedList path ) {
49
- PriorityQueue <String > arrivals = flights .get (departure );
50
- while (arrivals != null && !arrivals .isEmpty ()) {
51
- dfs (arrivals .poll (), flights , path );
52
- }
53
- path .addFirst (departure );
54
- }
55
-
56
-
57
- public static class MyOwnAttempt {
36
+ public static class Solution1 {
37
+ /** credit: https://discuss.leetcode.com/topic/36383/share-my-solution */
58
38
public List <String > findItinerary (String [][] tickets ) {
59
- List <List <String >> allPossibilities = new ArrayList <>();
60
- /**Find all tickets that start from JFK first*/
61
- List <String []> JFKStarts = new ArrayList <>();
62
- for (String [] ticket : tickets ) {
63
- if (ticket [0 ].equals ("JFK" )) {
64
- JFKStarts .add (ticket );
65
- }
66
- }
67
-
68
- for (String [] ticket : JFKStarts ) {
69
- List <String > thisPossibility = new ArrayList <>();
70
- thisPossibility .add (ticket [0 ]);
71
- thisPossibility .add (ticket [1 ]);
72
- dfs (ticket , thisPossibility , tickets , allPossibilities );
73
- }
74
-
75
- //sort lexicographically and return the smallest
76
- Collections .sort (allPossibilities , new ListComparator <>());
77
- return allPossibilities .get (0 );
78
- }
79
-
80
- private void dfs (String [] thisTicket , List <String > thisPossibility , String [][] tickets , List <List <String >> allPossibilities ) {
81
- if (thisPossibility .size () == tickets .length + 1 ) {
82
- allPossibilities .add (new ArrayList <>(thisPossibility ));
83
- return ;
84
- }
39
+ Map <String , PriorityQueue <String >> flights = new HashMap <>();
40
+ LinkedList <String > path = new LinkedList <>();
85
41
for (String [] ticket : tickets ) {
86
- if (!ticket .equals (thisTicket ) && thisPossibility .get (thisPossibility .size () - 1 ).equals (ticket [0 ])) {
87
- thisPossibility .add (ticket [1 ]);
88
- dfs (ticket , thisPossibility , tickets , allPossibilities );
89
- thisPossibility .remove (thisPossibility .size () - 1 );
90
- }
42
+ flights .putIfAbsent (ticket [0 ], new PriorityQueue <>());
43
+ flights .get (ticket [0 ]).add (ticket [1 ]);
91
44
}
45
+ dfs ("JFK" , flights , path );
46
+ return path ;
92
47
}
93
48
94
- private class ListComparator <T extends Comparable <T >> implements Comparator <List <T >> {
95
- @ Override
96
- public int compare (List <T > o1 , List <T > o2 ) {
97
- for (int i = 0 ; i < Math .min (o1 .size (), o2 .size ()); i ++) {
98
- int c = o1 .get (i ).compareTo (o2 .get (i ));
99
- if (c != 0 ) {
100
- return c ;
101
- }
102
- }
103
- return Integer .compare (o1 .size (), o2 .size ());
49
+ public void dfs (String departure , Map <String , PriorityQueue <String >> flights ,
50
+ LinkedList path ) {
51
+ PriorityQueue <String > arrivals = flights .get (departure );
52
+ while (arrivals != null && !arrivals .isEmpty ()) {
53
+ dfs (arrivals .poll (), flights , path );
104
54
}
55
+ path .addFirst (departure );
105
56
}
106
57
}
107
58
}
0 commit comments