Skip to content

Added tasks 3285-3292 #1822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3201_3300.s3285_find_indices_of_stable_mountains;

// #Easy #Array #2024_09_15_Time_1_ms_(100.00%)_Space_44.5_MB_(100.00%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
public List<Integer> stableMountains(int[] height, int threshold) {
int n = height.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
if (height[i] > threshold) {
list.add(i + 1);
}
}
return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3285\. Find Indices of Stable Mountains

Easy

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`.

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.

Return an array containing the indices of _all_ **stable** mountains in **any** order.

**Example 1:**

**Input:** height = [1,2,3,4,5], threshold = 2

**Output:** [3,4]

**Explanation:**

* Mountain 3 is stable because `height[2] == 3` is greater than `threshold == 2`.
* Mountain 4 is stable because `height[3] == 4` is greater than `threshold == 2`.

**Example 2:**

**Input:** height = [10,1,10,1,10], threshold = 3

**Output:** [1,3]

**Example 3:**

**Input:** height = [10,1,10,1,10], threshold = 10

**Output:** []

**Constraints:**

* `2 <= n == height.length <= 100`
* `1 <= height[i] <= 100`
* `1 <= threshold <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package g3201_3300.s3286_find_a_safe_walk_through_a_grid;

// #Medium #Array #Matrix #Heap_Priority_Queue #Graph #Shortest_Path #Breadth_First_Search
// #2024_09_15_Time_90_ms_(100.00%)_Space_46.6_MB_(100.00%)

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Queue;

public class Solution {
public boolean findSafeWalk(List<List<Integer>> grid, int health) {
int n = grid.size();
int m = grid.get(0).size();
int[] dr = new int[] {0, 0, 1, -1};
int[] dc = new int[] {1, -1, 0, 0};
boolean[][][] visited = new boolean[n][m][health + 1];
Queue<int[]> bfs = new LinkedList<>();
bfs.add(new int[] {0, 0, health - grid.get(0).get(0)});
visited[0][0][health - grid.get(0).get(0)] = true;
while (!bfs.isEmpty()) {
int size = bfs.size();
while (size-- > 0) {
int[] currNode = bfs.poll();
int r = Objects.requireNonNull(currNode)[0];
int c = currNode[1];
int h = currNode[2];
if (r == n - 1 && c == m - 1 && h > 0) {
return true;
}
for (int k = 0; k < 4; k++) {
int nr = r + dr[k];
int nc = c + dc[k];
if (isValidMove(nr, nc, n, m)) {
int nh = h - grid.get(nr).get(nc);
if (nh >= 0 && !visited[nr][nc][nh]) {
visited[nr][nc][nh] = true;
bfs.add(new int[] {nr, nc, nh});
}
}
}
}
}
return false;
}

private boolean isValidMove(int r, int c, int n, int m) {
return r >= 0 && c >= 0 && r < n && c < m;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
3286\. Find a Safe Walk Through a Grid

Medium

You are given an `m x n` binary matrix `grid` and an integer `health`.

You start on the upper-left corner `(0, 0)` and would like to get to the lower-right corner `(m - 1, n - 1)`.

You can move up, down, left, or right from one cell to another adjacent cell as long as your health _remains_ **positive**.

Cells `(i, j)` with `grid[i][j] = 1` are considered **unsafe** and reduce your health by 1.

Return `true` if you can reach the final cell with a health value of 1 or more, and `false` otherwise.

**Example 1:**

**Input:** grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]], health = 1

**Output:** true

**Explanation:**

The final cell can be reached safely by walking along the gray cells below.

![](https://assets.leetcode.com/uploads/2024/08/04/3868_examples_1drawio.png)

**Example 2:**

**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

**Output:** false

**Explanation:**

A minimum of 4 health points is needed to reach the final cell safely.

![](https://assets.leetcode.com/uploads/2024/08/04/3868_examples_2drawio.png)

**Example 3:**

**Input:** grid = [[1,1,1],[1,0,1],[1,1,1]], health = 5

**Output:** true

**Explanation:**

The final cell can be reached safely by walking along the gray cells below.

![](https://assets.leetcode.com/uploads/2024/08/04/3868_examples_3drawio.png)

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.

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 50`
* `2 <= m * n`
* `1 <= health <= m + n`
* `grid[i][j]` is either 0 or 1.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package g3201_3300.s3287_find_the_maximum_sequence_value_of_array;

// #Hard #Array #Dynamic_Programming #Bit_Manipulation
// #2024_09_15_Time_1140_ms_(100.00%)_Space_285.4_MB_(100.00%)

import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("unchecked")
public class Solution {
public int maxValue(int[] nums, int k) {
int n = nums.length;
Set<Integer>[][] left = new Set[n][k + 1];
Set<Integer>[][] right = new Set[n][k + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= k; j++) {
left[i][j] = new HashSet<>();
right[i][j] = new HashSet<>();
}
}
left[0][0].add(0);
left[0][1].add(nums[0]);
for (int i = 1; i < n - k; i++) {
left[i][0].add(0);
for (int j = 1; j <= k; j++) {
left[i][j].addAll(left[i - 1][j]);
for (int v : left[i - 1][j - 1]) {
left[i][j].add(v | nums[i]);
}
}
}
right[n - 1][0].add(0);
right[n - 1][1].add(nums[n - 1]);
int result = 0;
if (k == 1) {
for (int l : left[n - 2][k]) {
result = Math.max(result, l ^ nums[n - 1]);
}
}
for (int i = n - 2; i >= k; i--) {
right[i][0].add(0);
for (int j = 1; j <= k; j++) {
right[i][j].addAll(right[i + 1][j]);
for (int v : right[i + 1][j - 1]) {
right[i][j].add(v | nums[i]);
}
}
for (int l : left[i - 1][k]) {
for (int r : right[i][k]) {
result = Math.max(result, l ^ r);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
3287\. Find the Maximum Sequence Value of Array

Hard

You are given an integer array `nums` and a **positive** integer `k`.

The **value** of a sequence `seq` of size `2 * x` is defined as:

* `(seq[0] OR seq[1] OR ... OR seq[x - 1]) XOR (seq[x] OR seq[x + 1] OR ... OR seq[2 * x - 1])`.

Return the **maximum** **value** of any subsequence of `nums` having size `2 * k`.

**Example 1:**

**Input:** nums = [2,6,7], k = 1

**Output:** 5

**Explanation:**

The subsequence `[2, 7]` has the maximum value of `2 XOR 7 = 5`.

**Example 2:**

**Input:** nums = [4,2,5,6,7], k = 2

**Output:** 2

**Explanation:**

The subsequence `[4, 5, 6, 7]` has the maximum value of `(4 OR 5) XOR (6 OR 7) = 2`.

**Constraints:**

* `2 <= nums.length <= 400`
* <code>1 <= nums[i] < 2<sup>7</sup></code>
* `1 <= k <= nums.length / 2`
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package g3201_3300.s3288_length_of_the_longest_increasing_path;

// #Hard #Array #Sorting #Binary_Search #2024_09_15_Time_34_ms_(100.00%)_Space_106.2_MB_(50.00%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
public int maxPathLength(int[][] coordinates, int k) {
List<int[]> upper = new ArrayList<>();
List<int[]> lower = new ArrayList<>();
for (int[] pair : coordinates) {
if (pair[0] > coordinates[k][0] && pair[1] > coordinates[k][1]) {
upper.add(pair);
}
if (pair[0] < coordinates[k][0] && pair[1] < coordinates[k][1]) {
lower.add(pair);
}
}
upper.sort(
(a, b) -> {
if (a[0] == b[0]) {
return b[1] - a[1];
} else {
return a[0] - b[0];
}
});
lower.sort(
(a, b) -> {
if (a[0] == b[0]) {
return b[1] - a[1];
} else {
return a[0] - b[0];
}
});
return longestIncreasingLength(upper) + longestIncreasingLength(lower) + 1;
}

private int longestIncreasingLength(List<int[]> array) {
List<Integer> list = new ArrayList<>();
for (int[] pair : array) {
int m = list.size();
if (m == 0 || list.get(m - 1) < pair[1]) {
list.add(pair[1]);
} else {
int idx = binarySearch(list, pair[1]);
list.set(idx, pair[1]);
}
}
return list.size();
}

private int binarySearch(List<Integer> list, int target) {
int n = list.size();
int left = 0;
int right = n - 1;
while (left < right) {
int mid = (left + right) / 2;
if (list.get(mid) == target) {
return mid;
} else if (list.get(mid) > target) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3288\. Length of the Longest Increasing Path

Hard

You are given a 2D array of integers `coordinates` of length `n` and an integer `k`, where `0 <= k < n`.

<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.

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:

* <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`.
* <code>(x<sub>i</sub>, y<sub>i</sub>)</code> is in the given coordinates for all `i` where `1 <= i <= m`.

Return the **maximum** length of an **increasing path** that contains `coordinates[k]`.

**Example 1:**

**Input:** coordinates = [[3,1],[2,2],[4,1],[0,0],[5,3]], k = 1

**Output:** 3

**Explanation:**

`(0, 0)`, `(2, 2)`, `(5, 3)` is the longest increasing path that contains `(2, 2)`.

**Example 2:**

**Input:** coordinates = [[2,1],[7,0],[5,6]], k = 2

**Output:** 2

**Explanation:**

`(2, 1)`, `(5, 6)` is the longest increasing path that contains `(5, 6)`.

**Constraints:**

* <code>1 <= n == coordinates.length <= 10<sup>5</sup></code>
* `coordinates[i].length == 2`
* <code>0 <= coordinates[i][0], coordinates[i][1] <= 10<sup>9</sup></code>
* All elements in `coordinates` are **distinct**.
* `0 <= k <= n - 1`
Loading
Loading