Skip to content

Commit 5148c79

Browse files
authored
Added tasks 3618-3625
1 parent 8e20081 commit 5148c79

File tree

24 files changed

+1280
-0
lines changed

24 files changed

+1280
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_6_ms_(100.00%)_Space_78.20_MB_(81.48%)
5+
6+
import kotlin.math.abs
7+
8+
class Solution {
9+
fun splitArray(nums: IntArray): Long {
10+
val n = nums.size
11+
val isPrime = sieve(n)
12+
var sumA: Long = 0
13+
var sumB: Long = 0
14+
for (i in 0..<n) {
15+
if (isPrime[i]) {
16+
sumA += nums[i].toLong()
17+
} else {
18+
sumB += nums[i].toLong()
19+
}
20+
}
21+
return abs(sumA - sumB)
22+
}
23+
24+
// Sieve of Eratosthenes to find all prime indices up to n
25+
private fun sieve(n: Int): BooleanArray {
26+
val isPrime = BooleanArray(n)
27+
if (n > 2) {
28+
isPrime[2] = true
29+
}
30+
run {
31+
var i = 3
32+
while (i < n) {
33+
isPrime[i] = true
34+
i += 2
35+
}
36+
}
37+
if (n > 2) {
38+
isPrime[2] = true
39+
}
40+
var i = 3
41+
while (i * i < n) {
42+
if (isPrime[i]) {
43+
var j = i * i
44+
while (j < n) {
45+
isPrime[j] = false
46+
j += i * 2
47+
}
48+
}
49+
i += 2
50+
}
51+
isPrime[0] = false
52+
if (n > 1) {
53+
isPrime[1] = false
54+
}
55+
return isPrime
56+
}
57+
}
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 #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
4+
// #Biweekly_Contest_161 #2025_07_22_Time_14_ms_(100.00%)_Space_86.20_MB_(47.83%)
5+
6+
class Solution {
7+
private var m = 0
8+
private var n = 0
9+
10+
fun countIslands(grid: Array<IntArray>, k: Int): Int {
11+
var count = 0
12+
m = grid.size
13+
n = grid[0].size
14+
for (i in 0..<m) {
15+
for (j in 0..<n) {
16+
if (grid[i][j] != 0) {
17+
val curr = dfs(i, j, grid)
18+
if (curr % k == 0) {
19+
count++
20+
}
21+
}
22+
}
23+
}
24+
return count
25+
}
26+
27+
private fun dfs(i: Int, j: Int, grid: Array<IntArray>): Int {
28+
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] == 0) {
29+
return Int.Companion.MAX_VALUE
30+
}
31+
var count = grid[i][j]
32+
grid[i][j] = 0
33+
val x = dfs(i + 1, j, grid)
34+
val y = dfs(i, j + 1, grid)
35+
val a = dfs(i - 1, j, grid)
36+
val b = dfs(i, j - 1, grid)
37+
if (x != Int.Companion.MAX_VALUE) {
38+
count += x
39+
}
40+
if (y != Int.Companion.MAX_VALUE) {
41+
count += y
42+
}
43+
if (a != Int.Companion.MAX_VALUE) {
44+
count += a
45+
}
46+
if (b != Int.Companion.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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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_212_ms_(66.67%)_Space_150.09_MB_(13.33%)
5+
6+
import java.util.LinkedList
7+
import java.util.Queue
8+
import kotlin.math.max
9+
10+
class Solution {
11+
private fun topologicalSort(n: Int, g: Array<ArrayList<Int>>): List<Int> {
12+
val indeg = IntArray(n)
13+
for (i in 0 until n) {
14+
for (adjNode in g[i]) {
15+
indeg[adjNode]++
16+
}
17+
}
18+
val q: Queue<Int> = LinkedList()
19+
val ts = ArrayList<Int>()
20+
for (i in 0 until n) {
21+
if (indeg[i] == 0) {
22+
q.offer(i)
23+
}
24+
}
25+
while (!q.isEmpty()) {
26+
val u = q.poll()
27+
ts.add(u)
28+
for (v in g[u]) {
29+
indeg[v]--
30+
if (indeg[v] == 0) {
31+
q.offer(v)
32+
}
33+
}
34+
}
35+
return ts
36+
}
37+
38+
private fun check(
39+
x: Int,
40+
n: Int,
41+
adj: Array<ArrayList<IntArray>>,
42+
ts: List<Int>,
43+
online: BooleanArray,
44+
k: Long,
45+
): Boolean {
46+
val d = LongArray(n)
47+
d.fill(Long.MAX_VALUE)
48+
d[0] = 0
49+
for (u in ts) {
50+
if (d[u] != Long.MAX_VALUE) {
51+
for (p in adj[u]) {
52+
val v = p[0]
53+
val c = p[1]
54+
if (c < x || !online[v]) {
55+
continue
56+
}
57+
if (d[u] + c < d[v]) {
58+
d[v] = d[u] + c
59+
}
60+
}
61+
}
62+
}
63+
return d[n - 1] <= k
64+
}
65+
66+
fun findMaxPathScore(edges: Array<IntArray>, online: BooleanArray, k: Long): Int {
67+
val n = online.size
68+
// Adjacency list for graph with edge weights
69+
val adj = Array<ArrayList<IntArray>>(n) { ArrayList() }
70+
val g = Array<ArrayList<Int>>(n) { ArrayList() }
71+
for (e in edges) {
72+
val u = e[0]
73+
val v = e[1]
74+
val c = e[2]
75+
adj[u].add(intArrayOf(v, c))
76+
g[u].add(v)
77+
}
78+
val ts = topologicalSort(n, g)
79+
if (!check(0, n, adj, ts, online, k)) {
80+
return -1
81+
}
82+
var l = 0
83+
var h = 0
84+
for (e in edges) {
85+
h = max(h, e[2])
86+
}
87+
while (l < h) {
88+
val md = l + (h - l + 1) / 2
89+
if (check(md, n, adj, ts, online, k)) {
90+
l = md
91+
} else {
92+
h = md - 1
93+
}
94+
}
95+
return l
96+
}
97+
}
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)