Skip to content

Commit 71acd89

Browse files
authored
Added tasks 3285-3292
1 parent 862f069 commit 71acd89

File tree

24 files changed

+966
-0
lines changed

24 files changed

+966
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3201_3300.s3285_find_indices_of_stable_mountains;
2+
3+
// #Easy #Array #2024_09_15_Time_1_ms_(100.00%)_Space_44.5_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<Integer> stableMountains(int[] height, int threshold) {
10+
int n = height.length;
11+
List<Integer> list = new ArrayList<>();
12+
for (int i = 0; i < n - 1; i++) {
13+
if (height[i] > threshold) {
14+
list.add(i + 1);
15+
}
16+
}
17+
return list;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3285\. Find Indices of Stable Mountains
2+
3+
Easy
4+
5+
There are `n` mountains in a row, and each mountain has a height. You are given an integer array `height` where `height[i]` represents the height of mountain `i`, and an integer `threshold`.
6+
7+
A mountain is called **stable** if the mountain just before it (**if it exists**) has a height **strictly greater** than `threshold`. **Note** that mountain 0 is **not** stable.
8+
9+
Return an array containing the indices of _all_ **stable** mountains in **any** order.
10+
11+
**Example 1:**
12+
13+
**Input:** height = [1,2,3,4,5], threshold = 2
14+
15+
**Output:** [3,4]
16+
17+
**Explanation:**
18+
19+
* Mountain 3 is stable because `height[2] == 3` is greater than `threshold == 2`.
20+
* Mountain 4 is stable because `height[3] == 4` is greater than `threshold == 2`.
21+
22+
**Example 2:**
23+
24+
**Input:** height = [10,1,10,1,10], threshold = 3
25+
26+
**Output:** [1,3]
27+
28+
**Example 3:**
29+
30+
**Input:** height = [10,1,10,1,10], threshold = 10
31+
32+
**Output:** []
33+
34+
**Constraints:**
35+
36+
* `2 <= n == height.length <= 100`
37+
* `1 <= height[i] <= 100`
38+
* `1 <= threshold <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g3201_3300.s3286_find_a_safe_walk_through_a_grid;
2+
3+
// #Medium #Array #Matrix #Heap_Priority_Queue #Graph #Shortest_Path #Breadth_First_Search
4+
// #2024_09_15_Time_90_ms_(100.00%)_Space_46.6_MB_(100.00%)
5+
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Objects;
9+
import java.util.Queue;
10+
11+
public class Solution {
12+
public boolean findSafeWalk(List<List<Integer>> grid, int health) {
13+
int n = grid.size();
14+
int m = grid.get(0).size();
15+
int[] dr = new int[] {0, 0, 1, -1};
16+
int[] dc = new int[] {1, -1, 0, 0};
17+
boolean[][][] visited = new boolean[n][m][health + 1];
18+
Queue<int[]> bfs = new LinkedList<>();
19+
bfs.add(new int[] {0, 0, health - grid.get(0).get(0)});
20+
visited[0][0][health - grid.get(0).get(0)] = true;
21+
while (!bfs.isEmpty()) {
22+
int size = bfs.size();
23+
while (size-- > 0) {
24+
int[] currNode = bfs.poll();
25+
int r = Objects.requireNonNull(currNode)[0];
26+
int c = currNode[1];
27+
int h = currNode[2];
28+
if (r == n - 1 && c == m - 1 && h > 0) {
29+
return true;
30+
}
31+
for (int k = 0; k < 4; k++) {
32+
int nr = r + dr[k];
33+
int nc = c + dc[k];
34+
if (isValidMove(nr, nc, n, m)) {
35+
int nh = h - grid.get(nr).get(nc);
36+
if (nh >= 0 && !visited[nr][nc][nh]) {
37+
visited[nr][nc][nh] = true;
38+
bfs.add(new int[] {nr, nc, nh});
39+
}
40+
}
41+
}
42+
}
43+
}
44+
return false;
45+
}
46+
47+
private boolean isValidMove(int r, int c, int n, int m) {
48+
return r >= 0 && c >= 0 && r < n && c < m;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
3286\. Find a Safe Walk Through a Grid
2+
3+
Medium
4+
5+
You are given an `m x n` binary matrix `grid` and an integer `health`.
6+
7+
You start on the upper-left corner `(0, 0)` and would like to get to the lower-right corner `(m - 1, n - 1)`.
8+
9+
You can move up, down, left, or right from one cell to another adjacent cell as long as your health _remains_ **positive**.
10+
11+
Cells `(i, j)` with `grid[i][j] = 1` are considered **unsafe** and reduce your health by 1.
12+
13+
Return `true` if you can reach the final cell with a health value of 1 or more, and `false` otherwise.
14+
15+
**Example 1:**
16+
17+
**Input:** grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]], health = 1
18+
19+
**Output:** true
20+
21+
**Explanation:**
22+
23+
The final cell can be reached safely by walking along the gray cells below.
24+
25+
![](https://assets.leetcode.com/uploads/2024/08/04/3868_examples_1drawio.png)
26+
27+
**Example 2:**
28+
29+
**Input:** grid = [[0,1,1,0,0,0],[1,0,1,0,0,0],[0,1,1,1,0,1],[0,0,1,0,1,0]], health = 3
30+
31+
**Output:** false
32+
33+
**Explanation:**
34+
35+
A minimum of 4 health points is needed to reach the final cell safely.
36+
37+
![](https://assets.leetcode.com/uploads/2024/08/04/3868_examples_2drawio.png)
38+
39+
**Example 3:**
40+
41+
**Input:** grid = [[1,1,1],[1,0,1],[1,1,1]], health = 5
42+
43+
**Output:** true
44+
45+
**Explanation:**
46+
47+
The final cell can be reached safely by walking along the gray cells below.
48+
49+
![](https://assets.leetcode.com/uploads/2024/08/04/3868_examples_3drawio.png)
50+
51+
Any path that does not go through the cell `(1, 1)` is unsafe since your health will drop to 0 when reaching the final cell.
52+
53+
**Constraints:**
54+
55+
* `m == grid.length`
56+
* `n == grid[i].length`
57+
* `1 <= m, n <= 50`
58+
* `2 <= m * n`
59+
* `1 <= health <= m + n`
60+
* `grid[i][j]` is either 0 or 1.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g3201_3300.s3287_find_the_maximum_sequence_value_of_array;
2+
3+
// #Hard #Array #Dynamic_Programming #Bit_Manipulation
4+
// #2024_09_15_Time_1140_ms_(100.00%)_Space_285.4_MB_(100.00%)
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
@SuppressWarnings("unchecked")
10+
public class Solution {
11+
public int maxValue(int[] nums, int k) {
12+
int n = nums.length;
13+
Set<Integer>[][] left = new Set[n][k + 1];
14+
Set<Integer>[][] right = new Set[n][k + 1];
15+
for (int i = 0; i < n; i++) {
16+
for (int j = 0; j <= k; j++) {
17+
left[i][j] = new HashSet<>();
18+
right[i][j] = new HashSet<>();
19+
}
20+
}
21+
left[0][0].add(0);
22+
left[0][1].add(nums[0]);
23+
for (int i = 1; i < n - k; i++) {
24+
left[i][0].add(0);
25+
for (int j = 1; j <= k; j++) {
26+
left[i][j].addAll(left[i - 1][j]);
27+
for (int v : left[i - 1][j - 1]) {
28+
left[i][j].add(v | nums[i]);
29+
}
30+
}
31+
}
32+
right[n - 1][0].add(0);
33+
right[n - 1][1].add(nums[n - 1]);
34+
int result = 0;
35+
if (k == 1) {
36+
for (int l : left[n - 2][k]) {
37+
result = Math.max(result, l ^ nums[n - 1]);
38+
}
39+
}
40+
for (int i = n - 2; i >= k; i--) {
41+
right[i][0].add(0);
42+
for (int j = 1; j <= k; j++) {
43+
right[i][j].addAll(right[i + 1][j]);
44+
for (int v : right[i + 1][j - 1]) {
45+
right[i][j].add(v | nums[i]);
46+
}
47+
}
48+
for (int l : left[i - 1][k]) {
49+
for (int r : right[i][k]) {
50+
result = Math.max(result, l ^ r);
51+
}
52+
}
53+
}
54+
return result;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
3287\. Find the Maximum Sequence Value of Array
2+
3+
Hard
4+
5+
You are given an integer array `nums` and a **positive** integer `k`.
6+
7+
The **value** of a sequence `seq` of size `2 * x` is defined as:
8+
9+
* `(seq[0] OR seq[1] OR ... OR seq[x - 1]) XOR (seq[x] OR seq[x + 1] OR ... OR seq[2 * x - 1])`.
10+
11+
Return the **maximum** **value** of any subsequence of `nums` having size `2 * k`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,6,7], k = 1
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
The subsequence `[2, 7]` has the maximum value of `2 XOR 7 = 5`.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [4,2,5,6,7], k = 2
26+
27+
**Output:** 2
28+
29+
**Explanation:**
30+
31+
The subsequence `[4, 5, 6, 7]` has the maximum value of `(4 OR 5) XOR (6 OR 7) = 2`.
32+
33+
**Constraints:**
34+
35+
* `2 <= nums.length <= 400`
36+
* <code>1 <= nums[i] < 2<sup>7</sup></code>
37+
* `1 <= k <= nums.length / 2`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g3201_3300.s3288_length_of_the_longest_increasing_path;
2+
3+
// #Hard #Array #Sorting #Binary_Search #2024_09_15_Time_34_ms_(100.00%)_Space_106.2_MB_(50.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public int maxPathLength(int[][] coordinates, int k) {
10+
List<int[]> upper = new ArrayList<>();
11+
List<int[]> lower = new ArrayList<>();
12+
for (int[] pair : coordinates) {
13+
if (pair[0] > coordinates[k][0] && pair[1] > coordinates[k][1]) {
14+
upper.add(pair);
15+
}
16+
if (pair[0] < coordinates[k][0] && pair[1] < coordinates[k][1]) {
17+
lower.add(pair);
18+
}
19+
}
20+
upper.sort(
21+
(a, b) -> {
22+
if (a[0] == b[0]) {
23+
return b[1] - a[1];
24+
} else {
25+
return a[0] - b[0];
26+
}
27+
});
28+
lower.sort(
29+
(a, b) -> {
30+
if (a[0] == b[0]) {
31+
return b[1] - a[1];
32+
} else {
33+
return a[0] - b[0];
34+
}
35+
});
36+
return longestIncreasingLength(upper) + longestIncreasingLength(lower) + 1;
37+
}
38+
39+
private int longestIncreasingLength(List<int[]> array) {
40+
List<Integer> list = new ArrayList<>();
41+
for (int[] pair : array) {
42+
int m = list.size();
43+
if (m == 0 || list.get(m - 1) < pair[1]) {
44+
list.add(pair[1]);
45+
} else {
46+
int idx = binarySearch(list, pair[1]);
47+
list.set(idx, pair[1]);
48+
}
49+
}
50+
return list.size();
51+
}
52+
53+
private int binarySearch(List<Integer> list, int target) {
54+
int n = list.size();
55+
int left = 0;
56+
int right = n - 1;
57+
while (left < right) {
58+
int mid = (left + right) / 2;
59+
if (list.get(mid) == target) {
60+
return mid;
61+
} else if (list.get(mid) > target) {
62+
right = mid;
63+
} else {
64+
left = mid + 1;
65+
}
66+
}
67+
return left;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3288\. Length of the Longest Increasing Path
2+
3+
Hard
4+
5+
You are given a 2D array of integers `coordinates` of length `n` and an integer `k`, where `0 <= k < n`.
6+
7+
<code>coordinates[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> indicates the point <code>(x<sub>i</sub>, y<sub>i</sub>)</code> in a 2D plane.
8+
9+
An **increasing path** of length `m` is defined as a list of points <code>(x<sub>1</sub>, y<sub>1</sub>)</code>, <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, <code>(x<sub>3</sub>, y<sub>3</sub>)</code>, ..., <code>(x<sub>m</sub>, y<sub>m</sub>)</code> such that:
10+
11+
* <code>x<sub>i</sub> < x<sub>i + 1</sub></code> and <code>y<sub>i</sub> < y<sub>i + 1</sub></code> for all `i` where `1 <= i < m`.
12+
* <code>(x<sub>i</sub>, y<sub>i</sub>)</code> is in the given coordinates for all `i` where `1 <= i <= m`.
13+
14+
Return the **maximum** length of an **increasing path** that contains `coordinates[k]`.
15+
16+
**Example 1:**
17+
18+
**Input:** coordinates = [[3,1],[2,2],[4,1],[0,0],[5,3]], k = 1
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
`(0, 0)`, `(2, 2)`, `(5, 3)` is the longest increasing path that contains `(2, 2)`.
25+
26+
**Example 2:**
27+
28+
**Input:** coordinates = [[2,1],[7,0],[5,6]], k = 2
29+
30+
**Output:** 2
31+
32+
**Explanation:**
33+
34+
`(2, 1)`, `(5, 6)` is the longest increasing path that contains `(5, 6)`.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= n == coordinates.length <= 10<sup>5</sup></code>
39+
* `coordinates[i].length == 2`
40+
* <code>0 <= coordinates[i][0], coordinates[i][1] <= 10<sup>9</sup></code>
41+
* All elements in `coordinates` are **distinct**.
42+
* `0 <= k <= n - 1`

0 commit comments

Comments
 (0)