Skip to content

Commit aecb1d3

Browse files
committed
Added tasks 3618-3625
1 parent 8e20081 commit aecb1d3

File tree

24 files changed

+1240
-0
lines changed

24 files changed

+1240
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun splitArray(nums: IntArray): Long {
9+
val n = nums.size
10+
val isPrime = sieve(n)
11+
var sumA: Long = 0
12+
var sumB: Long = 0
13+
for (i in 0..<n) {
14+
if (isPrime[i]) {
15+
sumA += nums[i].toLong()
16+
} else {
17+
sumB += nums[i].toLong()
18+
}
19+
}
20+
return abs(sumA - sumB)
21+
}
22+
23+
// Sieve of Eratosthenes to find all prime indices up to n
24+
private fun sieve(n: Int): BooleanArray {
25+
val isPrime = BooleanArray(n)
26+
if (n > 2) {
27+
isPrime[2] = true
28+
}
29+
run {
30+
var i = 3
31+
while (i < n) {
32+
isPrime[i] = true
33+
i += 2
34+
}
35+
}
36+
if (n > 2) {
37+
isPrime[2] = true
38+
}
39+
var i = 3
40+
while (i * i < n) {
41+
if (isPrime[i]) {
42+
var j = i * i
43+
while (j < n) {
44+
isPrime[j] = false
45+
j += i * 2
46+
}
47+
}
48+
i += 2
49+
}
50+
isPrime[0] = false
51+
if (n > 1) {
52+
isPrime[1] = false
53+
}
54+
return isPrime
55+
}
56+
}
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+
class Solution {
6+
private var m = 0
7+
private var n = 0
8+
9+
fun countIslands(grid: Array<IntArray>, k: Int): Int {
10+
var count = 0
11+
m = grid.size
12+
n = grid[0].size
13+
for (i in 0..<m) {
14+
for (j in 0..<n) {
15+
if (grid[i][j] != 0) {
16+
val curr = dfs(i, j, grid)
17+
if (curr % k == 0) {
18+
count++
19+
}
20+
}
21+
}
22+
}
23+
return count
24+
}
25+
26+
private fun dfs(i: Int, j: Int, grid: Array<IntArray>): Int {
27+
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] == 0) {
28+
return Int.Companion.MAX_VALUE
29+
}
30+
var count = grid[i][j]
31+
grid[i][j] = 0
32+
val x = dfs(i + 1, j, grid)
33+
val y = dfs(i, j + 1, grid)
34+
val a = dfs(i - 1, j, grid)
35+
val b = dfs(i, j - 1, grid)
36+
if (x != Int.Companion.MAX_VALUE) {
37+
count += x
38+
}
39+
if (y != Int.Companion.MAX_VALUE) {
40+
count += y
41+
}
42+
if (a != Int.Companion.MAX_VALUE) {
43+
count += a
44+
}
45+
if (b != Int.Companion.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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g3601_3700.s3620_network_recovery_pathways
2+
3+
import kotlin.math.max
4+
import kotlin.math.min
5+
6+
// #Hard #2025_07_20_Time_18_ms_(100.00%)_Space_120.24_MB_(100.00%)
7+
8+
class Solution {
9+
private var ans = -1
10+
private var d = 0
11+
private var k: Long = 0
12+
13+
fun findMaxPathScore(edges: Array<IntArray>, online: BooleanArray, k: Long): Int {
14+
val n = online.size
15+
this.k = k
16+
this.d = n - 1
17+
val g: Array<ArrayList<P>> = Array(n) { ArrayList() }
18+
for (i in edges) {
19+
if (online[i[0]] && online[i[1]]) {
20+
g[i[0]].add(P(i[1], i[2]))
21+
}
22+
}
23+
dfs(g, 0, 0L, Int.Companion.MAX_VALUE)
24+
return ans
25+
}
26+
27+
private fun dfs(g: Array<ArrayList<P>>, s: Int, tc: Long, max: Int) {
28+
if (s == d) {
29+
if (ans == -1) {
30+
ans = max
31+
} else {
32+
ans = max(ans, max)
33+
}
34+
return
35+
}
36+
for (i in g[s]) {
37+
val cost = tc + i.c
38+
if (ans != -1 && ans >= max) {
39+
return
40+
}
41+
if (cost <= k) {
42+
dfs(g, i.d, cost, min(max, i.c))
43+
}
44+
}
45+
}
46+
47+
private class P(var d: Int, var c: Int)
48+
}
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)