Skip to content

Commit 99253cd

Browse files
refactor 332
1 parent 1db1c3c commit 99253cd

File tree

1 file changed

+4
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+4
-27
lines changed

src/main/java/com/fishercoder/solutions/_332.java

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,17 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.ArrayList;
4-
import java.util.Collections;
5-
import java.util.Comparator;
63
import java.util.HashMap;
74
import java.util.LinkedList;
85
import java.util.List;
96
import java.util.Map;
107
import java.util.PriorityQueue;
118

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-
*/
349
public class _332 {
3510

3611
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+
*/
3815
public List<String> findItinerary(String[][] tickets) {
3916
Map<String, PriorityQueue<String>> flights = new HashMap<>();
4017
LinkedList<String> path = new LinkedList<>();
@@ -47,7 +24,7 @@ public List<String> findItinerary(String[][] tickets) {
4724
}
4825

4926
public void dfs(String departure, Map<String, PriorityQueue<String>> flights,
50-
LinkedList path) {
27+
LinkedList path) {
5128
PriorityQueue<String> arrivals = flights.get(departure);
5229
while (arrivals != null && !arrivals.isEmpty()) {
5330
dfs(arrivals.poll(), flights, path);

0 commit comments

Comments
 (0)