Skip to content

Commit a3812cd

Browse files
authored
Added tasks 332, 334.
1 parent 804b21b commit a3812cd

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0301_0400.s0332_reconstruct_itinerary;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
public List<String> findItinerary(List<List<String>> tickets) {
11+
HashMap<String, PriorityQueue<String>> map = new HashMap<>();
12+
LinkedList<String> ans = new LinkedList<>();
13+
for (int i = 0; i < tickets.size(); i++) {
14+
String src = tickets.get(i).get(0);
15+
String dest = tickets.get(i).get(1);
16+
PriorityQueue<String> pq = map.getOrDefault(src, new PriorityQueue<>());
17+
pq.add(dest);
18+
map.put(src, pq);
19+
}
20+
dfs(map, "JFK", ans);
21+
return ans;
22+
}
23+
24+
private void dfs(Map<String, PriorityQueue<String>> map, String src, LinkedList<String> ans) {
25+
PriorityQueue<String> temp = map.get(src);
26+
while (temp != null && !temp.isEmpty()) {
27+
String nbr = temp.remove();
28+
dfs(map, nbr, ans);
29+
}
30+
ans.addFirst(src);
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
332\. Reconstruct Itinerary
2+
3+
Hard
4+
5+
You are given a list of airline `tickets` where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
6+
7+
All of the tickets belong to a man who departs from `"JFK"`, thus, the itinerary must begin with `"JFK"`. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.
8+
9+
* For example, the itinerary `["JFK", "LGA"]` has a smaller lexical order than `["JFK", "LGB"]`.
10+
11+
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg)
16+
17+
**Input:** tickets = \[\["MUC","LHR"\],\["JFK","MUC"\],\["SFO","SJC"\],\["LHR","SFO"\]\]
18+
19+
**Output:** \["JFK","MUC","LHR","SFO","SJC"\]
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg)
24+
25+
**Input:** tickets = \[\["JFK","SFO"\],\["JFK","ATL"\],\["SFO","ATL"\],\["ATL","JFK"\],\["ATL","SFO"\]\]
26+
27+
**Output:** \["JFK","ATL","JFK","SFO","ATL","SFO"\]
28+
29+
**Explanation:**
30+
31+
Another possible reconstruction is
32+
["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
33+
34+
**Constraints:**
35+
36+
* `1 <= tickets.length <= 300`
37+
* `tickets[i].length == 2`
38+
* <code>from<sub>i</sub>.length == 3</code>
39+
* <code>to<sub>i</sub>.length == 3</code>
40+
* <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.
41+
* <code>from<sub>i</sub> != to<sub>i</sub></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0301_0400.s0334_increasing_triplet_subsequence;
2+
3+
public class Solution {
4+
public boolean increasingTriplet(int[] nums) {
5+
int n = nums.length;
6+
int i = 0;
7+
int low = Integer.MAX_VALUE;
8+
int high = Integer.MAX_VALUE;
9+
while (i < n) {
10+
if (low >= nums[i]) {
11+
low = nums[i++];
12+
} else if (high >= nums[i]) {
13+
high = nums[i++];
14+
} else {
15+
return true;
16+
}
17+
}
18+
return false;
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
334\. Increasing Triplet Subsequence
2+
3+
Medium
4+
5+
Given an integer array `nums`, return `true` _if there exists a triple of indices_ `(i, j, k)` _such that_ `i < j < k` _and_ `nums[i] < nums[j] < nums[k]`. If no such indices exists, return `false`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = \[1,2,3,4,5\]
10+
11+
**Output:** true
12+
13+
**Explanation:** Any triplet where i < j < k is valid.
14+
15+
**Example 2:**
16+
17+
**Input:** nums = \[5,4,3,2,1\]
18+
19+
**Output:** false
20+
21+
**Explanation:** No triplet exists.
22+
23+
**Example 3:**
24+
25+
**Input:** nums = \[2,1,5,0,4,6\]
26+
27+
**Output:** true
28+
29+
**Explanation:** The triplet (3, 4, 5) is valid because nums\[3\] == 0 < nums\[4\] == 4 < nums\[5\] == 6.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= nums.length <= 5 * 10<sup>5</sup></code>
34+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
35+
36+
**Follow up:** Could you implement a solution that runs in `O(n)` time complexity and `O(1)` space complexity?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0301_0400.s0332_reconstruct_itinerary;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import org.junit.Test;
9+
10+
public class SolutionTest {
11+
@Test
12+
public void findItinerary() {
13+
List<List<String>> input =
14+
Arrays.asList(
15+
Arrays.asList("MUC", "LHR"),
16+
Arrays.asList("JFK", "MUC"),
17+
Arrays.asList("SFO", "SJC"),
18+
Arrays.asList("LHR", "SFO"));
19+
assertThat(
20+
new Solution().findItinerary(input),
21+
equalTo(Arrays.asList("JFK", "MUC", "LHR", "SFO", "SJC")));
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g0301_0400.s0334_increasing_triplet_subsequence;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void increasingTriplet() {
11+
assertThat(new Solution().increasingTriplet(new int[] {1, 2, 3, 4, 5}), equalTo(true));
12+
}
13+
}

0 commit comments

Comments
 (0)