Skip to content

Added version 3285-3292 #692

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 3 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,16 @@
package g3201_3300.s3285_find_indices_of_stable_mountains

// #Easy #Array #2024_09_17_Time_195_ms_(92.68%)_Space_37.5_MB_(48.78%)

class Solution {
fun stableMountains(height: IntArray, threshold: Int): List<Int> {
val n = height.size
val list: MutableList<Int> = mutableListOf()
for (i in 0 until n - 1) {
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,49 @@
package g3201_3300.s3286_find_a_safe_walk_through_a_grid

// #Medium #Array #Breadth_First_Search #Matrix #Heap_Priority_Queue #Graph #Shortest_Path
// #2024_09_17_Time_357_ms_(48.28%)_Space_48.2_MB_(58.62%)

import java.util.LinkedList
import java.util.Objects
import java.util.Queue

class Solution {
fun findSafeWalk(grid: List<List<Int>>, health: Int): Boolean {
val n = grid.size
val m = grid[0].size
val dr = intArrayOf(0, 0, 1, -1)
val dc = intArrayOf(1, -1, 0, 0)
val visited = Array<Array<BooleanArray>?>(n) { Array<BooleanArray>(m) { BooleanArray(health + 1) } }
val bfs: Queue<IntArray?> = LinkedList<IntArray>()
bfs.add(intArrayOf(0, 0, health - grid[0][0]))
visited[0]!![0][health - grid[0][0]] = true
while (bfs.isNotEmpty()) {
var size = bfs.size
while (size-- > 0) {
val currNode = bfs.poll()
val r = Objects.requireNonNull<IntArray>(currNode)[0]
val c = currNode!![1]
val h = currNode[2]
if (r == n - 1 && c == m - 1 && h > 0) {
return true
}
for (k in 0..3) {
val nr = r + dr[k]
val nc = c + dc[k]
if (isValidMove(nr, nc, n, m)) {
val nh: Int = h - grid[nr][nc]
if (nh >= 0 && !visited[nr]!![nc][nh]) {
visited[nr]!![nc][nh] = true
bfs.add(intArrayOf(nr, nc, nh))
}
}
}
}
}
return false
}

private fun isValidMove(r: Int, c: Int, n: Int, m: Int): Boolean {
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,50 @@
package g3201_3300.s3287_find_the_maximum_sequence_value_of_array

// #Hard #Array #Dynamic_Programming #Bit_Manipulation
// #2024_09_17_Time_2893_ms_(33.33%)_Space_290.4_MB_(33.33%)

import kotlin.math.max

class Solution {
fun maxValue(nums: IntArray, k: Int): Int {
val n = nums.size
val left: Array<Array<MutableSet<Int>>> =
Array<Array<MutableSet<Int>>>(n) { Array<MutableSet<Int>>(k + 1) { mutableSetOf() } }
val right: Array<Array<MutableSet<Int>>> =
Array<Array<MutableSet<Int>>>(n) { Array<MutableSet<Int>>(k + 1) { mutableSetOf() } }
left[0][0].add(0)
left[0][1].add(nums[0])
for (i in 1 until n - k) {
left[i][0].add(0)
for (j in 1..k) {
left[i][j].addAll(left[i - 1][j])
for (v in left[i - 1][j - 1]) {
left[i][j].add(v or nums[i])
}
}
}
right[n - 1][0].add(0)
right[n - 1][1].add(nums[n - 1])
var result = 0
if (k == 1) {
for (l in left[n - 2][k]) {
result = max(result, (l xor nums[n - 1]))
}
}
for (i in n - 2 downTo k) {
right[i][0].add(0)
for (j in 1..k) {
right[i][j].addAll(right[i + 1][j])
for (v in right[i + 1][j - 1]) {
right[i][j].add(v or nums[i])
}
}
for (l in left[i - 1][k]) {
for (r in right[i][k]) {
result = max(result, (l xor 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,71 @@
package g3201_3300.s3288_length_of_the_longest_increasing_path

// #Hard #Array #Sorting #Binary_Search #2024_09_17_Time_984_ms_(83.33%)_Space_147.1_MB_(16.67%)

import java.util.ArrayList
import java.util.Comparator

class Solution {
fun maxPathLength(coordinates: Array<IntArray>, k: Int): Int {
val upper: MutableList<IntArray> = ArrayList<IntArray>()
val lower: MutableList<IntArray> = ArrayList<IntArray>()
for (pair in 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.sortWith(
Comparator { a: IntArray, b: IntArray ->
if (a[0] == b[0]) {
b[1] - a[1]
} else {
a[0] - b[0]
}
}
)
lower.sortWith(
Comparator { a: IntArray, b: IntArray ->
if (a[0] == b[0]) {
b[1] - a[1]
} else {
a[0] - b[0]
}
}
)
return longestIncreasingLength(upper) + longestIncreasingLength(lower) + 1
}

private fun longestIncreasingLength(array: List<IntArray>): Int {
val list: MutableList<Int?> = ArrayList<Int?>()
for (pair in array) {
val m = list.size
if (m == 0 || list[m - 1]!! < pair[1]) {
list.add(pair[1])
} else {
val idx = binarySearch(list, pair[1])
list[idx] = pair[1]
}
}
return list.size
}

private fun binarySearch(list: List<Int?>, target: Int): Int {
val n = list.size
var left = 0
var right = n - 1
while (left < right) {
val mid = (left + right) / 2
if (list[mid] == target) {
return mid
} else if (list[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