Skip to content

Commit 15e30b8

Browse files
authored
Added tasks 3417-3420
1 parent fa61042 commit 15e30b8

File tree

12 files changed

+576
-0
lines changed

12 files changed

+576
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3401_3500.s3417_zigzag_grid_traversal_with_skip
2+
3+
// #Easy #Array #Matrix #Simulation #2025_01_14_Time_2_(100.00%)_Space_43.72_(76.92%)
4+
5+
class Solution {
6+
fun zigzagTraversal(grid: Array<IntArray>): List<Int> {
7+
val ans: MutableList<Int> = ArrayList<Int>()
8+
val m = grid.size
9+
val n = grid[0].size
10+
var i = 0
11+
var flag = true
12+
var skip = false
13+
while (i < m) {
14+
if (flag) {
15+
for (j in 0..<n) {
16+
if (!skip) {
17+
ans.add(grid[i][j])
18+
}
19+
skip = !skip
20+
}
21+
} else {
22+
for (j in n - 1 downTo 0) {
23+
if (!skip) {
24+
ans.add(grid[i][j])
25+
}
26+
skip = !skip
27+
}
28+
}
29+
flag = !flag
30+
i++
31+
}
32+
return ans
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3417\. Zigzag Grid Traversal With Skip
2+
3+
Easy
4+
5+
You are given an `m x n` 2D array `grid` of **positive** integers.
6+
7+
Your task is to traverse `grid` in a **zigzag** pattern while skipping every **alternate** cell.
8+
9+
Zigzag pattern traversal is defined as following the below actions:
10+
11+
* Start at the top-left cell `(0, 0)`.
12+
* Move _right_ within a row until the end of the row is reached.
13+
* Drop down to the next row, then traverse _left_ until the beginning of the row is reached.
14+
* Continue **alternating** between right and left traversal until every row has been traversed.
15+
16+
**Note** that you **must skip** every _alternate_ cell during the traversal.
17+
18+
Return an array of integers `result` containing, **in order**, the value of the cells visited during the zigzag traversal with skips.
19+
20+
**Example 1:**
21+
22+
**Input:** grid = [[1,2],[3,4]]
23+
24+
**Output:** [1,4]
25+
26+
**Explanation:**
27+
28+
**![](https://assets.leetcode.com/uploads/2024/11/23/4012_example0.png)**
29+
30+
**Example 2:**
31+
32+
**Input:** grid = [[2,1],[2,1],[2,1]]
33+
34+
**Output:** [2,1,2]
35+
36+
**Explanation:**
37+
38+
![](https://assets.leetcode.com/uploads/2024/11/23/4012_example1.png)
39+
40+
**Example 3:**
41+
42+
**Input:** grid = [[1,2,3],[4,5,6],[7,8,9]]
43+
44+
**Output:** [1,3,5,7,9]
45+
46+
**Explanation:**
47+
48+
![](https://assets.leetcode.com/uploads/2024/11/23/4012_example2.png)
49+
50+
**Constraints:**
51+
52+
* `2 <= n == grid.length <= 50`
53+
* `2 <= m == grid[i].length <= 50`
54+
* `1 <= grid[i][j] <= 2500`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g3401_3500.s3418_maximum_amount_of_money_robot_can_earn
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix #2025_01_14_Time_60_(81.82%)_Space_84.66_(81.82%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maximumAmount(coins: Array<IntArray>): Int {
9+
val m = coins.size
10+
val n = coins[0].size
11+
val dp = Array<IntArray>(m) { IntArray(n) }
12+
val dp1 = Array<IntArray>(m) { IntArray(n) }
13+
val dp2 = Array<IntArray>(m) { IntArray(n) }
14+
dp[0][0] = coins[0][0]
15+
for (j in 1..<n) {
16+
dp[0][j] = dp[0][j - 1] + coins[0][j]
17+
}
18+
for (i in 1..<m) {
19+
dp[i][0] = dp[i - 1][0] + coins[i][0]
20+
}
21+
for (i in 1..<m) {
22+
for (j in 1..<n) {
23+
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]) + coins[i][j]
24+
}
25+
}
26+
dp1[0][0] = max(coins[0][0], 0)
27+
for (j in 1..<n) {
28+
dp1[0][j] = max(dp[0][j - 1], (dp1[0][j - 1] + coins[0][j]))
29+
}
30+
for (i in 1..<m) {
31+
dp1[i][0] = max(dp[i - 1][0], (dp1[i - 1][0] + coins[i][0]))
32+
}
33+
for (i in 1..<m) {
34+
for (j in 1..<n) {
35+
dp1[i][j] = max(
36+
max(dp[i][j - 1], dp[i - 1][j]),
37+
(max(dp1[i][j - 1], dp1[i - 1][j]) + coins[i][j]),
38+
)
39+
}
40+
}
41+
dp2[0][0] = max(coins[0][0], 0)
42+
for (j in 1..<n) {
43+
dp2[0][j] = max(dp1[0][j - 1], (dp2[0][j - 1] + coins[0][j]))
44+
}
45+
for (i in 1..<m) {
46+
dp2[i][0] = max(dp1[i - 1][0], (dp2[i - 1][0] + coins[i][0]))
47+
}
48+
for (i in 1..<m) {
49+
for (j in 1..<n) {
50+
dp2[i][j] = max(
51+
max(dp1[i][j - 1], dp1[i - 1][j]),
52+
(max(dp2[i][j - 1], dp2[i - 1][j]) + coins[i][j]),
53+
)
54+
}
55+
}
56+
return dp2[m - 1][n - 1]
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3418\. Maximum Amount of Money Robot Can Earn
2+
3+
Medium
4+
5+
You are given an `m x n` grid. A robot starts at the top-left corner of the grid `(0, 0)` and wants to reach the bottom-right corner `(m - 1, n - 1)`. The robot can move either right or down at any point in time.
6+
7+
The grid contains a value `coins[i][j]` in each cell:
8+
9+
* If `coins[i][j] >= 0`, the robot gains that many coins.
10+
* If `coins[i][j] < 0`, the robot encounters a robber, and the robber steals the **absolute** value of `coins[i][j]` coins.
11+
12+
The robot has a special ability to **neutralize robbers** in at most **2 cells** on its path, preventing them from stealing coins in those cells.
13+
14+
**Note:** The robot's total coins can be negative.
15+
16+
Return the **maximum** profit the robot can gain on the route.
17+
18+
**Example 1:**
19+
20+
**Input:** coins = [[0,1,-1],[1,-2,3],[2,-3,4]]
21+
22+
**Output:** 8
23+
24+
**Explanation:**
25+
26+
An optimal path for maximum coins is:
27+
28+
1. Start at `(0, 0)` with `0` coins (total coins = `0`).
29+
2. Move to `(0, 1)`, gaining `1` coin (total coins = `0 + 1 = 1`).
30+
3. Move to `(1, 1)`, where there's a robber stealing `2` coins. The robot uses one neutralization here, avoiding the robbery (total coins = `1`).
31+
4. Move to `(1, 2)`, gaining `3` coins (total coins = `1 + 3 = 4`).
32+
5. Move to `(2, 2)`, gaining `4` coins (total coins = `4 + 4 = 8`).
33+
34+
**Example 2:**
35+
36+
**Input:** coins = [[10,10,10],[10,10,10]]
37+
38+
**Output:** 40
39+
40+
**Explanation:**
41+
42+
An optimal path for maximum coins is:
43+
44+
1. Start at `(0, 0)` with `10` coins (total coins = `10`).
45+
2. Move to `(0, 1)`, gaining `10` coins (total coins = `10 + 10 = 20`).
46+
3. Move to `(0, 2)`, gaining another `10` coins (total coins = `20 + 10 = 30`).
47+
4. Move to `(1, 2)`, gaining the final `10` coins (total coins = `30 + 10 = 40`).
48+
49+
**Constraints:**
50+
51+
* `m == coins.length`
52+
* `n == coins[i].length`
53+
* `1 <= m, n <= 500`
54+
* `-1000 <= coins[i][j] <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g3401_3500.s3419_minimize_the_maximum_edge_weight_of_graph
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Binary_Search #Graph #Shortest_Path
4+
// #2025_01_14_Time_88_(100.00%)_Space_115.26_(83.33%)
5+
6+
import java.util.LinkedList
7+
import java.util.Queue
8+
import kotlin.math.max
9+
10+
@Suppress("unused")
11+
class Solution {
12+
fun minMaxWeight(n: Int, edges: Array<IntArray>, threshold: Int): Int {
13+
val reversedG: Array<MutableList<IntArray>?> = arrayOfNulls<MutableList<IntArray>?>(n)
14+
for (i in 0..<n) {
15+
reversedG[i] = ArrayList<IntArray>()
16+
}
17+
for (i in edges) {
18+
val a = i[0]
19+
val b = i[1]
20+
val w = i[2]
21+
reversedG[b]!!.add(intArrayOf(a, w))
22+
}
23+
val distance = IntArray(n)
24+
distance.fill(Int.Companion.MAX_VALUE)
25+
distance[0] = 0
26+
if (reversedG[0]!!.isEmpty()) {
27+
return -1
28+
}
29+
val que: Queue<Int?> = LinkedList<Int?>()
30+
que.add(0)
31+
while (que.isNotEmpty()) {
32+
val cur: Int = que.poll()!!
33+
for (next in reversedG[cur]!!) {
34+
val node = next[0]
35+
val w = next[1]
36+
val nextdis = max(w, distance[cur])
37+
if (nextdis < distance[node]) {
38+
distance[node] = nextdis
39+
que.add(node)
40+
}
41+
}
42+
}
43+
var ans = 0
44+
for (i in 0..<n) {
45+
if (distance[i] == Int.Companion.MAX_VALUE) {
46+
return -1
47+
}
48+
ans = max(ans, distance[i])
49+
}
50+
return ans
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
3419\. Minimize the Maximum Edge Weight of Graph
2+
3+
Medium
4+
5+
You are given two integers, `n` and `threshold`, as well as a **directed** weighted graph of `n` nodes numbered from 0 to `n - 1`. The graph is represented by a **2D** integer array `edges`, where <code>edges[i] = [A<sub>i</sub>, B<sub>i</sub>, W<sub>i</sub>]</code> indicates that there is an edge going from node <code>A<sub>i</sub></code> to node <code>B<sub>i</sub></code> with weight <code>W<sub>i</sub></code>.
6+
7+
You have to remove some edges from this graph (possibly **none**), so that it satisfies the following conditions:
8+
9+
* Node 0 must be reachable from all other nodes.
10+
* The **maximum** edge weight in the resulting graph is **minimized**.
11+
* Each node has **at most** `threshold` outgoing edges.
12+
13+
Return the **minimum** possible value of the **maximum** edge weight after removing the necessary edges. If it is impossible for all conditions to be satisfied, return -1.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 5, edges = [[1,0,1],[2,0,2],[3,0,1],[4,3,1],[2,1,1]], threshold = 2
18+
19+
**Output:** 1
20+
21+
**Explanation:**
22+
23+
![](https://assets.leetcode.com/uploads/2024/12/09/s-1.png)
24+
25+
Remove the edge `2 -> 0`. The maximum weight among the remaining edges is 1.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 5, edges = [[0,1,1],[0,2,2],[0,3,1],[0,4,1],[1,2,1],[1,4,1]], threshold = 1
30+
31+
**Output:** \-1
32+
33+
**Explanation:**
34+
35+
It is impossible to reach node 0 from node 2.
36+
37+
**Example 3:**
38+
39+
**Input:** n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[3,4,2],[4,0,1]], threshold = 1
40+
41+
**Output:** 2
42+
43+
**Explanation:**
44+
45+
![](https://assets.leetcode.com/uploads/2024/12/09/s2-1.png)
46+
47+
Remove the edges `1 -> 3` and `1 -> 4`. The maximum weight among the remaining edges is 2.
48+
49+
**Example 4:**
50+
51+
**Input:** n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[4,0,1]], threshold = 1
52+
53+
**Output:** \-1
54+
55+
**Constraints:**
56+
57+
* <code>2 <= n <= 10<sup>5</sup></code>
58+
* `1 <= threshold <= n - 1`
59+
* <code>1 <= edges.length <= min(10<sup>5</sup>, n * (n - 1) / 2).</code>
60+
* `edges[i].length == 3`
61+
* <code>0 <= A<sub>i</sub>, B<sub>i</sub> < n</code>
62+
* <code>A<sub>i</sub> != B<sub>i</sub></code>
63+
* <code>1 <= W<sub>i</sub> <= 10<sup>6</sup></code>
64+
* There **may be** multiple edges between a pair of nodes, but they must have unique weights.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3401_3500.s3420_count_non_decreasing_subarrays_after_k_operations
2+
3+
// #Hard #Array #Two_Pointers #Stack #Monotonic_Stack #Queue #Segment_Tree #Monotonic_Queue
4+
// #2025_01_15_Time_28_(100.00%)_Space_68.93_(88.89%)
5+
6+
class Solution {
7+
fun countNonDecreasingSubarrays(nums: IntArray, k: Int): Long {
8+
val n = nums.size
9+
reverse(nums)
10+
var res: Long = 0
11+
var t = k.toLong()
12+
val q = IntArray(n + 1)
13+
var hh = 0
14+
var tt = -1
15+
var j = 0
16+
var i = 0
17+
while (j < n) {
18+
while (hh <= tt && nums[q[tt]] < nums[j]) {
19+
val r = q[tt--]
20+
val l = if (hh <= tt) q[tt] else i - 1
21+
t -= (r - l).toLong() * (nums[j] - nums[r])
22+
}
23+
q[++tt] = j
24+
while (t < 0) {
25+
t += (nums[q[hh]] - nums[i]).toLong()
26+
if (q[hh] == i) hh++
27+
i++
28+
}
29+
res += (j - i + 1).toLong()
30+
j++
31+
}
32+
return res
33+
}
34+
35+
private fun reverse(nums: IntArray) {
36+
val n = nums.size
37+
var i = 0
38+
var j = n - 1
39+
while (i < j) {
40+
val t = nums[i]
41+
nums[i] = nums[j]
42+
nums[j] = t
43+
i++
44+
j--
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)