Skip to content

Commit 41b9523

Browse files
committed
Added tasks 3618-3621
1 parent 815d3be commit 41b9523

File tree

12 files changed

+560
-0
lines changed

12 files changed

+560
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g3601_3700.s3618_split_array_by_prime_indices;
2+
3+
// #Medium #2025_07_20_Time_3_ms_(100.00%)_Space_62.52_MB_(100.00%)
4+
5+
public class Solution {
6+
public long splitArray(int[] nums) {
7+
int n = nums.length;
8+
boolean[] isPrime = sieve(n);
9+
long sumA = 0;
10+
long sumB = 0;
11+
for (int i = 0; i < n; i++) {
12+
if (isPrime[i]) {
13+
sumA += nums[i];
14+
} else {
15+
sumB += nums[i];
16+
}
17+
}
18+
return Math.abs(sumA - sumB);
19+
}
20+
21+
// Sieve of Eratosthenes to find all prime indices up to n
22+
private boolean[] sieve(int n) {
23+
boolean[] isPrime = new boolean[n];
24+
if (n > 2) {
25+
isPrime[2] = true;
26+
}
27+
for (int i = 3; i < n; i += 2) {
28+
isPrime[i] = true;
29+
}
30+
if (n > 2) {
31+
isPrime[2] = true;
32+
}
33+
for (int i = 3; i * i < n; i += 2) {
34+
if (isPrime[i]) {
35+
for (int j = i * i; j < n; j += i * 2) {
36+
isPrime[j] = false;
37+
}
38+
}
39+
}
40+
isPrime[0] = false;
41+
if (n > 1) {
42+
isPrime[1] = false;
43+
}
44+
return isPrime;
45+
}
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3618\. Split Array by Prime Indices
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
Split `nums` into two arrays `A` and `B` using the following rule:
8+
9+
* Elements at **prime** indices in `nums` must go into array `A`.
10+
* All other elements must go into array `B`.
11+
12+
Return the **absolute** difference between the sums of the two arrays: `|sum(A) - sum(B)|`.
13+
14+
**Note:** An empty array has a sum of 0.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,3,4]
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
* The only prime index in the array is 2, so `nums[2] = 4` is placed in array `A`.
25+
* The remaining elements, `nums[0] = 2` and `nums[1] = 3` are placed in array `B`.
26+
* `sum(A) = 4`, `sum(B) = 2 + 3 = 5`.
27+
* The absolute difference is `|4 - 5| = 1`.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [-1,5,7,0]
32+
33+
**Output:** 3
34+
35+
**Explanation:**
36+
37+
* The prime indices in the array are 2 and 3, so `nums[2] = 7` and `nums[3] = 0` are placed in array `A`.
38+
* The remaining elements, `nums[0] = -1` and `nums[1] = 5` are placed in array `B`.
39+
* `sum(A) = 7 + 0 = 7`, `sum(B) = -1 + 5 = 4`.
40+
* The absolute difference is `|7 - 4| = 3`.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
45+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g3601_3700.s3619_count_islands_with_total_value_divisible_by_k;
2+
3+
// #Medium #2025_07_20_Time_16_ms_(96.67%)_Space_70.90_MB_(100.00%)
4+
5+
public class Solution {
6+
private int m;
7+
private int n;
8+
9+
public int countIslands(int[][] grid, int k) {
10+
int count = 0;
11+
m = grid.length;
12+
n = grid[0].length;
13+
for (int i = 0; i < m; i++) {
14+
for (int j = 0; j < n; j++) {
15+
if (grid[i][j] != 0) {
16+
int curr = dfs(i, j, grid);
17+
if (curr % k == 0) {
18+
count++;
19+
}
20+
}
21+
}
22+
}
23+
return count;
24+
}
25+
26+
private int dfs(int i, int j, int[][] grid) {
27+
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] == 0) {
28+
return Integer.MAX_VALUE;
29+
}
30+
int count = grid[i][j];
31+
grid[i][j] = 0;
32+
int x = dfs(i + 1, j, grid);
33+
int y = dfs(i, j + 1, grid);
34+
int a = dfs(i - 1, j, grid);
35+
int b = dfs(i, j - 1, grid);
36+
if (x != Integer.MAX_VALUE) {
37+
count += x;
38+
}
39+
if (y != Integer.MAX_VALUE) {
40+
count += y;
41+
}
42+
if (a != Integer.MAX_VALUE) {
43+
count += a;
44+
}
45+
if (b != Integer.MAX_VALUE) {
46+
count += b;
47+
}
48+
return count;
49+
}
50+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3619\. Count Islands With Total Value Divisible by K
2+
3+
Medium
4+
5+
You are given an `m x n` matrix `grid` and a positive integer `k`. An **island** is a group of **positive** integers (representing land) that are **4-directionally** connected (horizontally or vertically).
6+
7+
The **total value** of an island is the sum of the values of all cells in the island.
8+
9+
Return the number of islands with a total value **divisible by** `k`.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2025/03/06/example1griddrawio-1.png)
14+
15+
**Input:** grid = [[0,2,1,0,0],[0,5,0,0,5],[0,0,1,0,0],[0,1,4,7,0],[0,2,0,0,8]], k = 5
16+
17+
**Output:** 2
18+
19+
**Explanation:**
20+
21+
The grid contains four islands. The islands highlighted in blue have a total value that is divisible by 5, while the islands highlighted in red do not.
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2025/03/06/example2griddrawio.png)
26+
27+
**Input:** grid = [[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3
28+
29+
**Output:** 6
30+
31+
**Explanation:**
32+
33+
The grid contains six islands, each with a total value that is divisible by 3.
34+
35+
**Constraints:**
36+
37+
* `m == grid.length`
38+
* `n == grid[i].length`
39+
* `1 <= m, n <= 1000`
40+
* <code>1 <= m * n <= 10<sup>5</sup></code>
41+
* <code>0 <= grid[i][j] <= 10<sup>6</sup></code>
42+
* <code>1 <= k <= 10<sup>6</sup></code>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g3601_3700.s3620_network_recovery_pathways;
2+
3+
// #Hard #2025_07_20_Time_18_ms_(100.00%)_Space_120.24_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
7+
@SuppressWarnings("unchecked")
8+
public class Solution {
9+
private int ans = -1;
10+
private int d;
11+
private long k = 0;
12+
13+
public int findMaxPathScore(int[][] edges, boolean[] online, long k) {
14+
int n = online.length;
15+
this.k = k;
16+
this.d = n - 1;
17+
ArrayList<P>[] g = new ArrayList[n];
18+
for (int i = 0; i < n; ++i) {
19+
g[i] = new ArrayList<>();
20+
}
21+
for (int[] i : edges) {
22+
if (online[i[0]] && online[i[1]]) {
23+
g[i[0]].add(new P(i[1], i[2]));
24+
}
25+
}
26+
dfs(g, 0, 0L, Integer.MAX_VALUE);
27+
return ans;
28+
}
29+
30+
private void dfs(ArrayList<P>[] g, int s, long tc, int max) {
31+
if (s == d) {
32+
if (ans == -1) {
33+
ans = max;
34+
} else {
35+
ans = Math.max(ans, max);
36+
}
37+
return;
38+
}
39+
for (P i : g[s]) {
40+
long cost = tc + i.c;
41+
if (ans != -1 && ans >= max) {
42+
return;
43+
}
44+
if (cost <= k) {
45+
dfs(g, i.d, cost, Math.min(max, i.c));
46+
}
47+
}
48+
}
49+
50+
private static final class P {
51+
int d;
52+
int c;
53+
54+
P(int d, int c) {
55+
this.d = d;
56+
this.c = c;
57+
}
58+
}
59+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
3620\. Network Recovery Pathways
2+
3+
Hard
4+
5+
You are given a directed acyclic graph of `n` nodes numbered from 0 to `n − 1`. This is represented by a 2D array `edges` of length `m`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, cost<sub>i</sub>]</code> indicates a one‑way communication from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code> with a recovery cost of <code>cost<sub>i</sub></code>.
6+
7+
Some nodes may be offline. You are given a boolean array `online` where `online[i] = true` means node `i` is online. Nodes 0 and `n − 1` are always online.
8+
9+
A path from 0 to `n − 1` is **valid** if:
10+
11+
* All intermediate nodes on the path are online.
12+
* The total recovery cost of all edges on the path does not exceed `k`.
13+
14+
For each valid path, define its **score** as the minimum edge‑cost along that path.
15+
16+
Return the **maximum** path score (i.e., the largest **minimum**\-edge cost) among all valid paths. If no valid path exists, return -1.
17+
18+
**Example 1:**
19+
20+
**Input:** edges = [[0,1,5],[1,3,10],[0,2,3],[2,3,4]], online = [true,true,true,true], k = 10
21+
22+
**Output:** 3
23+
24+
**Explanation:**
25+
26+
![](https://assets.leetcode.com/uploads/2025/06/06/graph-10.png)
27+
28+
* The graph has two possible routes from node 0 to node 3:
29+
30+
1. Path `0 → 1 → 3`
31+
32+
* Total cost = `5 + 10 = 15`, which exceeds k (`15 > 10`), so this path is invalid.
33+
34+
2. Path `0 → 2 → 3`
35+
36+
* Total cost = `3 + 4 = 7 <= k`, so this path is valid.
37+
38+
* The minimum edge‐cost along this path is `min(3, 4) = 3`.
39+
40+
* There are no other valid paths. Hence, the maximum among all valid path‐scores is 3.
41+
42+
43+
**Example 2:**
44+
45+
**Input:** edges = [[0,1,7],[1,4,5],[0,2,6],[2,3,6],[3,4,2],[2,4,6]], online = [true,true,true,false,true], k = 12
46+
47+
**Output:** 6
48+
49+
**Explanation:**
50+
51+
![](https://assets.leetcode.com/uploads/2025/06/06/graph-11.png)
52+
53+
* Node 3 is offline, so any path passing through 3 is invalid.
54+
55+
* Consider the remaining routes from 0 to 4:
56+
57+
1. Path `0 → 1 → 4`
58+
59+
* Total cost = `7 + 5 = 12 <= k`, so this path is valid.
60+
61+
* The minimum edge‐cost along this path is `min(7, 5) = 5`.
62+
63+
2. Path `0 → 2 → 3 → 4`
64+
65+
* Node 3 is offline, so this path is invalid regardless of cost.
66+
67+
3. Path `0 → 2 → 4`
68+
69+
* Total cost = `6 + 6 = 12 <= k`, so this path is valid.
70+
71+
* The minimum edge‐cost along this path is `min(6, 6) = 6`.
72+
73+
* Among the two valid paths, their scores are 5 and 6. Therefore, the answer is 6.
74+
75+
76+
**Constraints:**
77+
78+
* `n == online.length`
79+
* <code>2 <= n <= 5 * 10<sup>4</sup></code>
80+
* `0 <= m == edges.length <=` <code>min(10<sup>5</sup>, n * (n - 1) / 2)</code>
81+
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, cost<sub>i</sub>]</code>
82+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
83+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
84+
* <code>0 <= cost<sub>i</sub> <= 10<sup>9</sup></code>
85+
* <code>0 <= k <= 5 * 10<sup>13</sup></code>
86+
* `online[i]` is either `true` or `false`, and both `online[0]` and `online[n − 1]` are `true`.
87+
* The given graph is a directed acyclic graph.

0 commit comments

Comments
 (0)