Skip to content

Commit 418d111

Browse files
committed
Added tasks 3417-3435
1 parent afd9e22 commit 418d111

File tree

19 files changed

+2387
-360
lines changed
  • src/main/java/g3401_3500
    • s3414_maximum_score_of_non_overlapping_intervals
    • s3417_zigzag_grid_traversal_with_skip
    • s3418_maximum_amount_of_money_robot_can_earn
    • s3419_minimize_the_maximum_edge_weight_of_graph
    • s3420_count_non_decreasing_subarrays_after_k_operations
    • s3421_find_students_who_improved
    • s3423_maximum_difference_between_adjacent_elements_in_a_circular_array
    • s3424_minimum_cost_to_make_arrays_identical
    • s3425_longest_special_path
    • s3426_manhattan_distances_of_all_arrangements_of_pieces
    • s3427_sum_of_variable_length_subarrays
    • s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences
    • s3429_paint_house_iv
    • s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays
    • s3432_count_partitions_with_even_sum_difference
    • s3433_count_mentions_per_user
    • s3434_maximum_frequency_after_subarray_operation
    • s3435_frequencies_of_shortest_supersequences

19 files changed

+2387
-360
lines changed

README.md

Lines changed: 644 additions & 359 deletions
Large diffs are not rendered by default.

src/main/java/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class Solution {
5959
}
6060
int[][] dp1 = new int[n][0];
6161
long[] dp = new long[n];
62-
Arrays.sort(ns, (a, b) -> (a[0] - b[0]));
62+
Arrays.sort(ns, (a, b) -> a[0] - b[0]);
6363
for (int k = 0; k < 4; ++k) {
6464
int[][] dp3 = new int[n][];
6565
long[] dp2 = new long[n];
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3417\. Zigzag Grid Traversal With Skip
5+
6+
Easy
7+
8+
You are given an `m x n` 2D array `grid` of **positive** integers.
9+
10+
Your task is to traverse `grid` in a **zigzag** pattern while skipping every **alternate** cell.
11+
12+
Zigzag pattern traversal is defined as following the below actions:
13+
14+
* Start at the top-left cell `(0, 0)`.
15+
* Move _right_ within a row until the end of the row is reached.
16+
* Drop down to the next row, then traverse _left_ until the beginning of the row is reached.
17+
* Continue **alternating** between right and left traversal until every row has been traversed.
18+
19+
**Note** that you **must skip** every _alternate_ cell during the traversal.
20+
21+
Return an array of integers `result` containing, **in order**, the value of the cells visited during the zigzag traversal with skips.
22+
23+
**Example 1:**
24+
25+
**Input:** grid = \[\[1,2],[3,4]]
26+
27+
**Output:** [1,4]
28+
29+
**Explanation:**
30+
31+
**![](https://assets.leetcode.com/uploads/2024/11/23/4012_example0.png)**
32+
33+
**Example 2:**
34+
35+
**Input:** grid = \[\[2,1],[2,1],[2,1]]
36+
37+
**Output:** [2,1,2]
38+
39+
**Explanation:**
40+
41+
![](https://assets.leetcode.com/uploads/2024/11/23/4012_example1.png)
42+
43+
**Example 3:**
44+
45+
**Input:** grid = \[\[1,2,3],[4,5,6],[7,8,9]]
46+
47+
**Output:** [1,3,5,7,9]
48+
49+
**Explanation:**
50+
51+
![](https://assets.leetcode.com/uploads/2024/11/23/4012_example2.png)
52+
53+
**Constraints:**
54+
55+
* `2 <= n == grid.length <= 50`
56+
* `2 <= m == grid[i].length <= 50`
57+
* `1 <= grid[i][j] <= 2500`
58+
59+
## Solution
60+
61+
```java
62+
import java.util.ArrayList;
63+
import java.util.List;
64+
65+
public class Solution {
66+
public List<Integer> zigzagTraversal(int[][] grid) {
67+
List<Integer> ans = new ArrayList<>();
68+
int m = grid.length;
69+
int n = grid[0].length;
70+
int i = 0;
71+
boolean flag = true;
72+
boolean skip = false;
73+
while (i < m) {
74+
if (flag) {
75+
for (int j = 0; j < n; j++) {
76+
if (!skip) {
77+
ans.add(grid[i][j]);
78+
}
79+
skip = !skip;
80+
}
81+
} else {
82+
for (int j = n - 1; j >= 0; j--) {
83+
if (!skip) {
84+
ans.add(grid[i][j]);
85+
}
86+
skip = !skip;
87+
}
88+
}
89+
flag = !flag;
90+
i++;
91+
}
92+
return ans;
93+
}
94+
}
95+
```
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3418\. Maximum Amount of Money Robot Can Earn
5+
6+
Medium
7+
8+
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.
9+
10+
The grid contains a value `coins[i][j]` in each cell:
11+
12+
* If `coins[i][j] >= 0`, the robot gains that many coins.
13+
* If `coins[i][j] < 0`, the robot encounters a robber, and the robber steals the **absolute** value of `coins[i][j]` coins.
14+
15+
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.
16+
17+
**Note:** The robot's total coins can be negative.
18+
19+
Return the **maximum** profit the robot can gain on the route.
20+
21+
**Example 1:**
22+
23+
**Input:** coins = \[\[0,1,-1],[1,-2,3],[2,-3,4]]
24+
25+
**Output:** 8
26+
27+
**Explanation:**
28+
29+
An optimal path for maximum coins is:
30+
31+
1. Start at `(0, 0)` with `0` coins (total coins = `0`).
32+
2. Move to `(0, 1)`, gaining `1` coin (total coins = `0 + 1 = 1`).
33+
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`).
34+
4. Move to `(1, 2)`, gaining `3` coins (total coins = `1 + 3 = 4`).
35+
5. Move to `(2, 2)`, gaining `4` coins (total coins = `4 + 4 = 8`).
36+
37+
**Example 2:**
38+
39+
**Input:** coins = \[\[10,10,10],[10,10,10]]
40+
41+
**Output:** 40
42+
43+
**Explanation:**
44+
45+
An optimal path for maximum coins is:
46+
47+
1. Start at `(0, 0)` with `10` coins (total coins = `10`).
48+
2. Move to `(0, 1)`, gaining `10` coins (total coins = `10 + 10 = 20`).
49+
3. Move to `(0, 2)`, gaining another `10` coins (total coins = `20 + 10 = 30`).
50+
4. Move to `(1, 2)`, gaining the final `10` coins (total coins = `30 + 10 = 40`).
51+
52+
**Constraints:**
53+
54+
* `m == coins.length`
55+
* `n == coins[i].length`
56+
* `1 <= m, n <= 500`
57+
* `-1000 <= coins[i][j] <= 1000`
58+
59+
## Solution
60+
61+
```java
62+
public class Solution {
63+
public int maximumAmount(int[][] coins) {
64+
int m = coins.length;
65+
int n = coins[0].length;
66+
int[][] dp = new int[m][n];
67+
int[][] dp1 = new int[m][n];
68+
int[][] dp2 = new int[m][n];
69+
dp[0][0] = coins[0][0];
70+
for (int j = 1; j < n; j++) {
71+
dp[0][j] = dp[0][j - 1] + coins[0][j];
72+
}
73+
for (int i = 1; i < m; i++) {
74+
dp[i][0] = dp[i - 1][0] + coins[i][0];
75+
}
76+
for (int i = 1; i < m; i++) {
77+
for (int j = 1; j < n; j++) {
78+
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]) + coins[i][j];
79+
}
80+
}
81+
dp1[0][0] = Math.max(coins[0][0], 0);
82+
for (int j = 1; j < n; j++) {
83+
dp1[0][j] = Math.max(dp[0][j - 1], dp1[0][j - 1] + coins[0][j]);
84+
}
85+
for (int i = 1; i < m; i++) {
86+
dp1[i][0] = Math.max(dp[i - 1][0], dp1[i - 1][0] + coins[i][0]);
87+
}
88+
for (int i = 1; i < m; i++) {
89+
for (int j = 1; j < n; j++) {
90+
dp1[i][j] =
91+
Math.max(
92+
Math.max(dp[i][j - 1], dp[i - 1][j]),
93+
Math.max(dp1[i][j - 1], dp1[i - 1][j]) + coins[i][j]);
94+
}
95+
}
96+
dp2[0][0] = Math.max(coins[0][0], 0);
97+
for (int j = 1; j < n; j++) {
98+
dp2[0][j] = Math.max(dp1[0][j - 1], dp2[0][j - 1] + coins[0][j]);
99+
}
100+
for (int i = 1; i < m; i++) {
101+
dp2[i][0] = Math.max(dp1[i - 1][0], dp2[i - 1][0] + coins[i][0]);
102+
}
103+
for (int i = 1; i < m; i++) {
104+
for (int j = 1; j < n; j++) {
105+
dp2[i][j] =
106+
Math.max(
107+
Math.max(dp1[i][j - 1], dp1[i - 1][j]),
108+
Math.max(dp2[i][j - 1], dp2[i - 1][j]) + coins[i][j]);
109+
}
110+
}
111+
return dp2[m - 1][n - 1];
112+
}
113+
}
114+
```
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3419\. Minimize the Maximum Edge Weight of Graph
5+
6+
Medium
7+
8+
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>.
9+
10+
You have to remove some edges from this graph (possibly **none**), so that it satisfies the following conditions:
11+
12+
* Node 0 must be reachable from all other nodes.
13+
* The **maximum** edge weight in the resulting graph is **minimized**.
14+
* Each node has **at most** `threshold` outgoing edges.
15+
16+
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.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 5, edges = \[\[1,0,1],[2,0,2],[3,0,1],[4,3,1],[2,1,1]], threshold = 2
21+
22+
**Output:** 1
23+
24+
**Explanation:**
25+
26+
![](https://assets.leetcode.com/uploads/2024/12/09/s-1.png)
27+
28+
Remove the edge `2 -> 0`. The maximum weight among the remaining edges is 1.
29+
30+
**Example 2:**
31+
32+
**Input:** n = 5, edges = \[\[0,1,1],[0,2,2],[0,3,1],[0,4,1],[1,2,1],[1,4,1]], threshold = 1
33+
34+
**Output:** \-1
35+
36+
**Explanation:**
37+
38+
It is impossible to reach node 0 from node 2.
39+
40+
**Example 3:**
41+
42+
**Input:** n = 5, edges = \[\[1,2,1],[1,3,3],[1,4,5],[2,3,2],[3,4,2],[4,0,1]], threshold = 1
43+
44+
**Output:** 2
45+
46+
**Explanation:**
47+
48+
![](https://assets.leetcode.com/uploads/2024/12/09/s2-1.png)
49+
50+
Remove the edges `1 -> 3` and `1 -> 4`. The maximum weight among the remaining edges is 2.
51+
52+
**Example 4:**
53+
54+
**Input:** n = 5, edges = \[\[1,2,1],[1,3,3],[1,4,5],[2,3,2],[4,0,1]], threshold = 1
55+
56+
**Output:** \-1
57+
58+
**Constraints:**
59+
60+
* <code>2 <= n <= 10<sup>5</sup></code>
61+
* `1 <= threshold <= n - 1`
62+
* <code>1 <= edges.length <= min(10<sup>5</sup>, n * (n - 1) / 2).</code>
63+
* `edges[i].length == 3`
64+
* <code>0 <= A<sub>i</sub>, B<sub>i</sub> < n</code>
65+
* <code>A<sub>i</sub> != B<sub>i</sub></code>
66+
* <code>1 <= W<sub>i</sub> <= 10<sup>6</sup></code>
67+
* There **may be** multiple edges between a pair of nodes, but they must have unique weights.
68+
69+
## Solution
70+
71+
```java
72+
import java.util.ArrayList;
73+
import java.util.Arrays;
74+
import java.util.LinkedList;
75+
import java.util.Queue;
76+
77+
@SuppressWarnings({"unchecked", "unused", "java:S1172"})
78+
public class Solution {
79+
private ArrayList<ArrayList<Pair>> revadj;
80+
81+
private static class Pair {
82+
int node;
83+
int weight;
84+
85+
public Pair(int node, int weight) {
86+
this.node = node;
87+
this.weight = weight;
88+
}
89+
}
90+
91+
public int minMaxWeight(int n, int[][] edges, int threshold) {
92+
ArrayList<ArrayList<Pair>> adj = new ArrayList<>();
93+
revadj = new ArrayList<>();
94+
for (int i = 0; i <= n + 1; i++) {
95+
adj.add(new ArrayList<>());
96+
revadj.add(new ArrayList<>());
97+
}
98+
for (int[] edge : edges) {
99+
int u = edge[0];
100+
int v = edge[1];
101+
int wt = edge[2];
102+
adj.get(u).add(new Pair(v, wt));
103+
revadj.get(v).add(new Pair(u, wt));
104+
}
105+
if (!check(n)) {
106+
return -1;
107+
}
108+
int[] dist = new int[n + 1];
109+
Arrays.fill(dist, (int) (1e9));
110+
dist[0] = 0;
111+
Queue<Pair> q = new LinkedList<>();
112+
q.offer(new Pair(0, 0));
113+
while (!q.isEmpty()) {
114+
int u = q.peek().node;
115+
int currMax = q.peek().weight;
116+
q.poll();
117+
for (int i = 0; i < revadj.get(u).size(); i++) {
118+
int v = revadj.get(u).get(i).node;
119+
int wt = revadj.get(u).get(i).weight;
120+
if (dist[v] > Math.max(wt, currMax)) {
121+
dist[v] = Math.max(wt, currMax);
122+
q.offer(new Pair(v, dist[v]));
123+
}
124+
}
125+
}
126+
int maxi = dist[0];
127+
for (int i = 0; i < n; i++) {
128+
maxi = Math.max(maxi, dist[i]);
129+
}
130+
return maxi;
131+
}
132+
133+
private boolean check(int n) {
134+
int[] vis = new int[n];
135+
ArrayList<Integer> nodes = new ArrayList<>();
136+
dfs(0, vis, nodes);
137+
return nodes.size() == n;
138+
}
139+
140+
private void dfs(int u, int[] vis, ArrayList<Integer> nodes) {
141+
nodes.add(u);
142+
vis[u] = 1;
143+
for (int i = 0; i < revadj.get(u).size(); i++) {
144+
int v = revadj.get(u).get(i).node;
145+
if (vis[v] == 0) {
146+
dfs(v, vis, nodes);
147+
}
148+
}
149+
}
150+
}
151+
```

0 commit comments

Comments
 (0)