forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindItenerary.java
89 lines (72 loc) · 3.03 KB
/
findItenerary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Given a list of airline tickets represented by pairs of departure and arrival airports
[from, to], reconstruct the itinerary in order. All of the tickets belong to a man who
departs from JFK. Thus, the itinerary must begin with JFK.
Note:
If there are multiple valid itineraries, you should return the itinerary that has
the smallest lexical order when read as a single string. For example, the itinerary
["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
All airports are represented by three capital letters (IATA code).
You may assume all tickets form at least one valid itinerary.
Example 1:
Input: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Output: ["JFK", "MUC", "LHR", "SFO", "SJC"]
//1. build a graph
//2. sort vertices
//3. run dfs, with backtracking
//4 return route
TC:
SC: O(V+E)
public class Solution {
private HashMap<String, List<String>> adjList = new HashMap<>(); //graph
private LinkedList<String> route = new LinkedList<>(); /res
private int numTickets = 0;
private int numTicketsUsed = 0;
public List<String> findItinerary(String[][] tickets) {
if (tickets == null || tickets.length == 0) return route;
// build graph
numTickets = tickets.length;
for (int i = 0; i < tickets.length; ++i) {
if (!adjList.containsKey(tickets[i][0])) {
// create a new list
List<String> list = new ArrayList<>();
list.add(tickets[i][1]);
adjList.put(tickets[i][0], list);
} else {
// add to existing list
adjList.get(tickets[i][0]).add(tickets[i][1]);
}
}
// sort vertices in the adjacency list so they appear in lexical order
for (Map.Entry<String, List<String>> entry : adjList.entrySet()) {
Collections.sort(entry.getValue());
}
// start DFS
route.add("JFK");
dfsRoute("JFK");
return route;
}
private void dfsRoute(String v) {
// base case: vertex v is not in adjacency list
// v is not a starting point in any itinerary, or we would have stored it
// thus we have reached end point in our DFS
if (!adjList.containsKey(v)) return;
List<String> list = adjList.get(v);
for (int i = 0; i < list.size(); ++i) {
String neighbor = list.get(i);
// remove ticket(route) from graph
list.remove(i);
route.add(neighbor);
numTicketsUsed++;
dfsRoute(neighbor);
// we only return when we have used all tickets
if (numTickets == numTicketsUsed) return;
// otherwise we need to revert the changes and try other tickets
list.add(i, neighbor);
// This line took me a long time to debug
// we must remove the last airport, since in an itinerary, the same
// airport can appear many times!!
route.removeLast();
numTicketsUsed--;
}
}
}