Skip to content

Commit a80f39d

Browse files
committed
Added 1 solution & modified 1 solution
1 parent 4100cfb commit a80f39d

2 files changed

Lines changed: 80 additions & 21 deletions

File tree

‎Medium/Reconstruct Itinerary.java‎

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
11
class Solution {
2-
public List<String> findItinerary(List<List<String>> tickets) {
3-
Map<String, PriorityQueue<String>> map = new HashMap<>();
4-
for (List<String> ticket : tickets) {
5-
map.computeIfAbsent(ticket.get(0), k -> new PriorityQueue<>()).add(ticket.get(1));
6-
}
7-
8-
List<String> route = new ArrayList<>();
9-
Stack<String> stack = new Stack<>();
10-
11-
stack.push("JFK");
12-
13-
while (!stack.isEmpty()) {
14-
while (map.containsKey(stack.peek()) && !map.get(stack.peek()).isEmpty()) {
15-
stack.push(map.get(stack.peek()).poll());
16-
}
17-
18-
route.add(stack.pop());
2+
List<String> result = null;
3+
public List<String> findItinerary(List<List<String>> tickets) {
4+
Map<String, List<String>> map = new HashMap<>();
5+
Map<String, boolean[]> visitMap = new HashMap<>();
6+
int flights = tickets.size();
7+
for (List<String> ticket : tickets) {
8+
map.computeIfAbsent(ticket.get(0), k -> new ArrayList<>()).add(ticket.get(1));
9+
}
10+
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
11+
Collections.sort(entry.getValue());
12+
visitMap.put(entry.getKey(), new boolean[entry.getValue().size()]);
13+
}
14+
LinkedList<String> route = new LinkedList<String>();
15+
route.add("JFK");
16+
backtrack(map, visitMap, flights, route, "JFK");
17+
return result;
18+
}
19+
20+
private boolean backtrack(
21+
Map<String, List<String>> map,
22+
Map<String, boolean[]> visitMap,
23+
int flights,
24+
LinkedList<String> route,
25+
String origin
26+
) {
27+
if (route.size() == (flights + 1)) {
28+
result = (List<String>) route.clone();
29+
return true;
30+
}
31+
if (!map.containsKey(origin)) {
32+
return false;
33+
}
34+
int i = 0;
35+
boolean[] visited = visitMap.get(origin);
36+
for (String dest : map.get(origin)) {
37+
if (!visited[i]) {
38+
visited[i] = true;
39+
route.add(dest);
40+
boolean res = backtrack(map, visitMap, flights, route, dest);
41+
route.pollLast();
42+
visited[i] = false;
43+
if (res == true) {
44+
return true;
1945
}
20-
21-
Collections.reverse(route);
22-
23-
return route;
46+
}
47+
i++;
2448
}
49+
return false;
50+
}
2551
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode removeZeroSumSublists(ListNode head) {
13+
ListNode dummy = new ListNode(0);
14+
ListNode curr = head;
15+
dummy.next = head;
16+
Map<Integer, ListNode> map = new HashMap<>();
17+
int prefixSum = 0;
18+
map.put(0, dummy);
19+
while (curr != null) {
20+
prefixSum += curr.val;
21+
map.put(prefixSum, curr);
22+
curr = curr.next;
23+
}
24+
prefixSum = 0;
25+
curr = dummy;
26+
while (curr != null) {
27+
prefixSum += curr.val;
28+
curr.next = map.get(prefixSum).next;
29+
curr = curr.next;
30+
}
31+
return dummy.next;
32+
}
33+
}

0 commit comments

Comments
 (0)