Skip to content

Commit e94e895

Browse files
committed
Added tasks 3417-3420
1 parent 302f471 commit e94e895

File tree

12 files changed

+542
-0
lines changed

12 files changed

+542
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g3401_3500.s3417_zigzag_grid_traversal_with_skip;
2+
3+
// #Easy #2025_01_12_Time_1_(100.00%)_Space_45.64_(_%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<Integer> zigzagTraversal(int[][] grid) {
10+
List<Integer> ans = new ArrayList<>();
11+
int m = grid.length;
12+
int n = grid[0].length;
13+
int i = 0;
14+
boolean flag = true, skip = false;
15+
while (i < m) {
16+
if (flag) {
17+
for (int j = 0; j < n; j++) {
18+
if (!skip) {
19+
ans.add(grid[i][j]);
20+
}
21+
skip = !skip;
22+
}
23+
} else {
24+
for (int j = n - 1; j >= 0; j--) {
25+
if (!skip) {
26+
ans.add(grid[i][j]);
27+
}
28+
skip = !skip;
29+
}
30+
}
31+
flag = !flag;
32+
i++;
33+
}
34+
return ans;
35+
}
36+
}
Lines changed: 54 additions & 0 deletions
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`
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g3401_3500.s3418_maximum_amount_of_money_robot_can_earn;
2+
3+
// #Medium #2025_01_12_Time_156_(_%)_Space_79.73_(_%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
private int solve(int[][] coins, int r, int c, int i, int j, int skip, int[][][] dp) {
9+
if (i == r - 1 && j == c - 1) {
10+
if (coins[i][j] < 0 && skip > 0) {
11+
return 0;
12+
}
13+
return coins[i][j];
14+
} else if (dp[i][j][skip] != Integer.MIN_VALUE) {
15+
return dp[i][j][skip];
16+
}
17+
int val1 = Integer.MIN_VALUE;
18+
int val2 = Integer.MIN_VALUE;
19+
if (i < r - 1) {
20+
if (coins[i][j] < 0 && skip > 0) {
21+
val1 =
22+
Math.max(
23+
solve(coins, r, c, i + 1, j, skip - 1, dp),
24+
coins[i][j] + solve(coins, r, c, i + 1, j, skip, dp));
25+
} else {
26+
val1 = coins[i][j] + solve(coins, r, c, i + 1, j, skip, dp);
27+
}
28+
}
29+
if (j < c - 1) {
30+
if (coins[i][j] < 0 && skip > 0) {
31+
val2 =
32+
Math.max(
33+
solve(coins, r, c, i, j + 1, skip - 1, dp),
34+
coins[i][j] + solve(coins, r, c, i, j + 1, skip, dp));
35+
} else {
36+
val2 = coins[i][j] + solve(coins, r, c, i, j + 1, skip, dp);
37+
}
38+
}
39+
40+
return dp[i][j][skip] = Math.max(val1, val2);
41+
}
42+
43+
public int maximumAmount(int[][] coins) {
44+
int r = coins.length;
45+
int c = coins[0].length;
46+
int[][][] dp = new int[r][c][3];
47+
for (int[][] arr1 : dp) {
48+
for (int[] arr2 : arr1) {
49+
Arrays.fill(arr2, Integer.MIN_VALUE);
50+
}
51+
}
52+
return solve(coins, r, c, 0, 0, 2, dp);
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
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`
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package g3401_3500.s3419_minimize_the_maximum_edge_weight_of_graph;
2+
3+
// #Medium #2025_01_12_Time_1014_(100.00%)_Space_114.21_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Queue;
9+
10+
@SuppressWarnings({"unused", "java:S1172"})
11+
public class Solution {
12+
public int minMaxWeight(int n, int[][] edges, int threshold) {
13+
int minW = Integer.MAX_VALUE;
14+
int maxW = 0;
15+
for (int[] e : edges) {
16+
minW = Math.min(minW, e[2]);
17+
maxW = Math.max(maxW, e[2]);
18+
}
19+
int ans = -1;
20+
int l = minW;
21+
int h = maxW;
22+
while (l <= h) {
23+
int mid = (l + h) >> 1;
24+
if (find(n, edges, mid)) {
25+
ans = mid;
26+
h = mid - 1;
27+
} else l = mid + 1;
28+
}
29+
30+
return ans;
31+
}
32+
33+
private boolean find(int n, int[][] edges, int maxi) {
34+
List<List<Integer>> adj = new ArrayList<>();
35+
for (int i = 0; i < n; i++) {
36+
adj.add(new ArrayList<>());
37+
}
38+
for (int[] e : edges) {
39+
if (e[2] <= maxi) {
40+
adj.get(e[0]).add(e[1]);
41+
}
42+
}
43+
return zero(n, adj);
44+
}
45+
46+
public boolean zero(int n, List<List<Integer>> adj) {
47+
List<List<Integer>> rev = new ArrayList<>();
48+
for (int i = 0; i < n; i++) {
49+
rev.add(new ArrayList<>());
50+
}
51+
for (int i = 0; i < n; i++) {
52+
for (int nb : adj.get(i)) {
53+
rev.get(nb).add(i);
54+
}
55+
}
56+
boolean[] vis = new boolean[n];
57+
Queue<Integer> q = new LinkedList<>();
58+
q.add(0);
59+
int cnt = 1;
60+
vis[0] = true;
61+
while (!q.isEmpty()) {
62+
int curr = q.poll();
63+
for (int nb : rev.get(curr)) {
64+
if (!vis[nb]) {
65+
vis[nb] = true;
66+
q.add(nb);
67+
cnt++;
68+
}
69+
}
70+
}
71+
return cnt == n;
72+
}
73+
}
Lines changed: 64 additions & 0 deletions
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.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g3401_3500.s3420_count_non_decreasing_subarrays_after_k_operations;
2+
3+
// #Hard #2025_01_12_Time_31_(100.00%)_Space_62.21_(100.00%)
4+
5+
import java.util.ArrayDeque;
6+
import java.util.Deque;
7+
8+
public class Solution {
9+
public long countNonDecreasingSubarrays(int[] nums, long k) {
10+
// Reverse the array
11+
int n = nums.length;
12+
for (int i = 0; i < n / 2; ++i) {
13+
int temp = nums[i];
14+
nums[i] = nums[n - 1 - i];
15+
nums[n - 1 - i] = temp;
16+
}
17+
18+
long res = 0;
19+
Deque<Integer> q = new ArrayDeque<>();
20+
for (int j = 0, i = 0; j < nums.length; ++j) {
21+
while (!q.isEmpty() && nums[q.peekLast()] < nums[j]) {
22+
int r = q.pollLast();
23+
int l = q.isEmpty() ? i - 1 : q.peekLast();
24+
k -= (long) (r - l) * (nums[j] - nums[r]);
25+
}
26+
q.addLast(j);
27+
while (k < 0) {
28+
k += nums[q.peekFirst()] - nums[i];
29+
if (q.peekFirst() == i) {
30+
q.pollFirst();
31+
}
32+
++i;
33+
}
34+
res += j - i + 1;
35+
}
36+
return res;
37+
}
38+
}

0 commit comments

Comments
 (0)