Skip to content

Commit 0c85cf2

Browse files
authored
Added tasks 3618-3625
1 parent 815d3be commit 0c85cf2

File tree

24 files changed

+1237
-0
lines changed

24 files changed

+1237
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3601_3700.s3618_split_array_by_prime_indices;
2+
3+
// #Medium #Array #Math #Number_Theory #Biweekly_Contest_161
4+
// #2025_07_22_Time_3_ms_(100.00%)_Space_62.61_MB_(10.13%)
5+
6+
public class Solution {
7+
public long splitArray(int[] nums) {
8+
int n = nums.length;
9+
boolean[] isPrime = sieve(n);
10+
long sumA = 0;
11+
long sumB = 0;
12+
for (int i = 0; i < n; i++) {
13+
if (isPrime[i]) {
14+
sumA += nums[i];
15+
} else {
16+
sumB += nums[i];
17+
}
18+
}
19+
return Math.abs(sumA - sumB);
20+
}
21+
22+
// Sieve of Eratosthenes to find all prime indices up to n
23+
private boolean[] sieve(int n) {
24+
boolean[] isPrime = new boolean[n];
25+
if (n > 2) {
26+
isPrime[2] = true;
27+
}
28+
for (int i = 3; i < n; i += 2) {
29+
isPrime[i] = true;
30+
}
31+
if (n > 2) {
32+
isPrime[2] = true;
33+
}
34+
for (int i = 3; i * i < n; i += 2) {
35+
if (isPrime[i]) {
36+
for (int j = i * i; j < n; j += i * 2) {
37+
isPrime[j] = false;
38+
}
39+
}
40+
}
41+
isPrime[0] = false;
42+
if (n > 1) {
43+
isPrime[1] = false;
44+
}
45+
return isPrime;
46+
}
47+
}
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g3601_3700.s3619_count_islands_with_total_value_divisible_by_k;
2+
3+
// #Medium #Array #Matrix #Union_Find #Biweekly_Contest_161 #Depth_First_Search
4+
// #Breadth_First_Search #2025_07_22_Time_16_ms_(96.65%)_Space_70.96_MB_(50.08%)
5+
6+
public class Solution {
7+
private int m;
8+
private int n;
9+
10+
public int countIslands(int[][] grid, int k) {
11+
int count = 0;
12+
m = grid.length;
13+
n = grid[0].length;
14+
for (int i = 0; i < m; i++) {
15+
for (int j = 0; j < n; j++) {
16+
if (grid[i][j] != 0) {
17+
int curr = dfs(i, j, grid);
18+
if (curr % k == 0) {
19+
count++;
20+
}
21+
}
22+
}
23+
}
24+
return count;
25+
}
26+
27+
private int dfs(int i, int j, int[][] grid) {
28+
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] == 0) {
29+
return Integer.MAX_VALUE;
30+
}
31+
int count = grid[i][j];
32+
grid[i][j] = 0;
33+
int x = dfs(i + 1, j, grid);
34+
int y = dfs(i, j + 1, grid);
35+
int a = dfs(i - 1, j, grid);
36+
int b = dfs(i, j - 1, grid);
37+
if (x != Integer.MAX_VALUE) {
38+
count += x;
39+
}
40+
if (y != Integer.MAX_VALUE) {
41+
count += y;
42+
}
43+
if (a != Integer.MAX_VALUE) {
44+
count += a;
45+
}
46+
if (b != Integer.MAX_VALUE) {
47+
count += b;
48+
}
49+
return count;
50+
}
51+
}
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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package g3601_3700.s3620_network_recovery_pathways;
2+
3+
// #Hard #Array #Dynamic_Programming #Binary_Search #Heap_Priority_Queue #Graph #Topological_Sort
4+
// #Shortest_Path #Biweekly_Contest_161 #2025_07_22_Time_151_ms_(66.08%)_Space_108.87_MB_(63.77%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
import java.util.Queue;
11+
12+
public class Solution {
13+
private List<Integer> topologicalSort(int n, List<List<Integer>> g) {
14+
int[] indeg = new int[n];
15+
for (int i = 0; i < n; ++i) {
16+
for (int adjNode : g.get(i)) {
17+
indeg[adjNode]++;
18+
}
19+
}
20+
Queue<Integer> q = new LinkedList<>();
21+
List<Integer> ts = new ArrayList<>();
22+
for (int i = 0; i < n; ++i) {
23+
if (indeg[i] == 0) {
24+
q.offer(i);
25+
}
26+
}
27+
while (!q.isEmpty()) {
28+
int u = q.poll();
29+
ts.add(u);
30+
for (int v : g.get(u)) {
31+
indeg[v]--;
32+
if (indeg[v] == 0) {
33+
q.offer(v);
34+
}
35+
}
36+
}
37+
return ts;
38+
}
39+
40+
private boolean check(
41+
int x, int n, List<List<int[]>> adj, List<Integer> ts, boolean[] online, long k) {
42+
long[] d = new long[n];
43+
Arrays.fill(d, Long.MAX_VALUE);
44+
d[0] = 0;
45+
for (int u : ts) {
46+
// If d[u] is reachable
47+
if (d[u] != Long.MAX_VALUE) {
48+
for (int[] p : adj.get(u)) {
49+
int v = p[0];
50+
int c = p[1];
51+
if (c < x || !online[v]) {
52+
continue;
53+
}
54+
if (d[u] + c < d[v]) {
55+
d[v] = d[u] + c;
56+
}
57+
}
58+
}
59+
}
60+
return d[n - 1] <= k;
61+
}
62+
63+
public int findMaxPathScore(int[][] edges, boolean[] online, long k) {
64+
int n = online.length;
65+
// Adjacency list for graph with edge weights
66+
List<List<int[]>> adj = new ArrayList<>();
67+
for (int i = 0; i < n; i++) {
68+
adj.add(new ArrayList<>());
69+
}
70+
List<List<Integer>> g = new ArrayList<>();
71+
for (int i = 0; i < n; i++) {
72+
g.add(new ArrayList<>());
73+
}
74+
for (int[] e : edges) {
75+
int u = e[0];
76+
int v = e[1];
77+
int c = e[2];
78+
adj.get(u).add(new int[] {v, c});
79+
g.get(u).add(v);
80+
}
81+
List<Integer> ts = topologicalSort(n, g);
82+
if (!check(0, n, adj, ts, online, k)) {
83+
return -1;
84+
}
85+
int l = 0;
86+
int h = 0;
87+
for (int[] e : edges) {
88+
h = Math.max(h, e[2]);
89+
}
90+
while (l < h) {
91+
int md = l + (h - l + 1) / 2;
92+
if (check(md, n, adj, ts, online, k)) {
93+
l = md;
94+
} else {
95+
h = md - 1;
96+
}
97+
}
98+
return l;
99+
}
100+
}
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)