Skip to content

Commit 14e26bc

Browse files
authored
Added tasks 3648-3655
1 parent 93df966 commit 14e26bc

File tree

26 files changed

+1123
-1
lines changed

26 files changed

+1123
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package g3601_3700.s3648_minimum_sensors_to_cover_grid
2+
3+
// #Medium #Biweekly_Contest_163 #2025_08_17_Time_0_ms_(100.00%)_Space_41.03_MB_(100.00%)
4+
5+
class Solution {
6+
fun minSensors(n: Int, m: Int, k: Int): Int {
7+
val size = k * 2 + 1
8+
val x = n / size + (if (n % size == 0) 0 else 1)
9+
val y = m / size + (if (m % size == 0) 0 else 1)
10+
return x * y
11+
}
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
3648\. Minimum Sensors to Cover Grid
2+
3+
Medium
4+
5+
You are given `n × m` grid and an integer `k`.
6+
7+
A sensor placed on cell `(r, c)` covers all cells whose **Chebyshev distance** from `(r, c)` is **at most** `k`.
8+
9+
The **Chebyshev distance** between two cells <code>(r<sub>1</sub>, c<sub>1</sub>)</code> and <code>(r<sub>2</sub>, c<sub>2</sub>)</code> is <code>max(|r<sub>1</sub> − r<sub>2</sub>|,|c<sub>1</sub> − c<sub>2</sub>|)</code>.
10+
11+
Your task is to return the **minimum** number of sensors required to cover every cell of the grid.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 5, m = 5, k = 1
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
Placing sensors at positions `(0, 3)`, `(1, 0)`, `(3, 3)`, and `(4, 1)` ensures every cell in the grid is covered. Thus, the answer is 4.
22+
23+
**Example 2:**
24+
25+
**Input:** n = 2, m = 2, k = 2
26+
27+
**Output:** 1
28+
29+
**Explanation:**
30+
31+
With `k = 2`, a single sensor can cover the entire `2 * 2` grid regardless of its position. Thus, the answer is 1.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= n <= 10<sup>3</sup></code>
36+
* <code>1 <= m <= 10<sup>3</sup></code>
37+
* <code>0 <= k <= 10<sup>3</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3601_3700.s3649_number_of_perfect_pairs
2+
3+
// #Medium #Biweekly_Contest_163 #2025_08_17_Time_46_ms_(100.00%)_Space_60.00_MB_(100.00%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun perfectPairs(nums: IntArray): Long {
9+
val n = nums.size
10+
val arr = LongArray(n)
11+
for (i in 0..<n) {
12+
arr[i] = abs(nums[i].toLong())
13+
}
14+
arr.sort()
15+
var cnt: Long = 0
16+
var r = 0
17+
for (i in 0..<n) {
18+
if (r < i) {
19+
r = i
20+
}
21+
while (r + 1 < n && arr[r + 1] <= 2 * arr[i]) {
22+
r++
23+
}
24+
cnt += (r - i).toLong()
25+
}
26+
return cnt
27+
}
28+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
3649\. Number of Perfect Pairs
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
A pair of indices `(i, j)` is called **perfect** if the following conditions are satisfied:
8+
9+
* `i < j`
10+
* Let `a = nums[i]`, `b = nums[j]`. Then:
11+
* `min(|a - b|, |a + b|) <= min(|a|, |b|)`
12+
* `max(|a - b|, |a + b|) >= max(|a|, |b|)`
13+
14+
Return the number of **distinct** perfect pairs.
15+
16+
**Note:** The absolute value `|x|` refers to the **non-negative** value of `x`.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [0,1,2,3]
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
There are 2 perfect pairs:
27+
28+
| `(i, j)` | `(a, b)` | `min(|a − b|, |a + b|)` | `min(|a|, |b|)` | `max(|a − b|, |a + b|)` | `max(|a|, |b|)` |
29+
|----------|-----------|-------------------------------------|-----------------|-------------------------------------|-----------------|
30+
| (1, 2) | (1, 2) | `min(|1 − 2|, |1 + 2|) = 1` | 1 | `max(|1 − 2|, |1 + 2|) = 3` | 2 |
31+
| (2, 3) | (2, 3) | `min(|2 − 3|, |2 + 3|) = 1` | 2 | `max(|2 − 3|, |2 + 3|) = 5` | 3 |
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [-3,2,-1,4]
36+
37+
**Output:** 4
38+
39+
**Explanation:**
40+
41+
There are 4 perfect pairs:
42+
43+
| `(i, j)` | `(a, b)` | `min(|a − b|, |a + b|)` | `min(|a|, |b|)` | `max(|a − b|, |a + b|)` | `max(|a|, |b|)` |
44+
|----------|-----------|-----------------------------------------------|-----------------|-----------------------------------------------|-----------------|
45+
| (0, 1) | (-3, 2) | `min(|-3 - 2|, |-3 + 2|) = 1` | 2 | `max(|-3 - 2|, |-3 + 2|) = 5` | 3 |
46+
| (0, 3) | (-3, 4) | `min(|-3 - 4|, |-3 + 4|) = 1` | 3 | `max(|-3 - 4|, |-3 + 4|) = 7` | 4 |
47+
| (1, 2) | (2, -1) | `min(|2 - (-1)|, |2 + (-1)|) = 1` | 1 | `max(|2 - (-1)|, |2 + (-1)|) = 3` | 2 |
48+
| (1, 3) | (2, 4) | `min(|2 - 4|, |2 + 4|) = 2` | 2 | `max(|2 - 4|, |2 + 4|) = 6` | 4 |
49+
50+
**Example 3:**
51+
52+
**Input:** nums = [1,10,100,1000]
53+
54+
**Output:** 0
55+
56+
**Explanation:**
57+
58+
There are no perfect pairs. Thus, the answer is 0.
59+
60+
**Constraints:**
61+
62+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
63+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package g3601_3700.s3650_minimum_cost_path_with_edge_reversals
2+
3+
// #Medium #Biweekly_Contest_163 #2025_08_17_Time_51_ms_(99.85%)_Space_110.03_MB_(49.54%)
4+
5+
import java.util.PriorityQueue
6+
7+
class Solution {
8+
private var cnt = 0
9+
private lateinit var head: IntArray
10+
private lateinit var next: IntArray
11+
private lateinit var to: IntArray
12+
private lateinit var weight: IntArray
13+
14+
private class Dist(var u: Int, var d: Int) : Comparable<Dist> {
15+
override fun compareTo(other: Dist): Int {
16+
return d.toLong().compareTo(other.d.toLong())
17+
}
18+
}
19+
20+
private fun init(n: Int, m: Int) {
21+
head = IntArray(n)
22+
head.fill(-1)
23+
next = IntArray(m)
24+
to = IntArray(m)
25+
weight = IntArray(m)
26+
}
27+
28+
private fun add(u: Int, v: Int, w: Int) {
29+
to[cnt] = v
30+
weight[cnt] = w
31+
next[cnt] = head[u]
32+
head[u] = cnt++
33+
}
34+
35+
private fun dist(s: Int, t: Int, n: Int): Int {
36+
val queue: PriorityQueue<Dist> = PriorityQueue<Dist>()
37+
val dist = IntArray(n)
38+
dist.fill(INF)
39+
dist[s] = 0
40+
queue.add(Dist(s, dist[s]))
41+
while (queue.isNotEmpty()) {
42+
val d = queue.remove()
43+
val u = d.u
44+
if (dist[u] < d.d) {
45+
continue
46+
}
47+
if (u == t) {
48+
return dist[t]
49+
}
50+
var i = head[u]
51+
while (i != -1) {
52+
val v = to[i]
53+
val w = weight[i]
54+
if (dist[v] > dist[u] + w) {
55+
dist[v] = dist[u] + w
56+
queue.add(Dist(v, dist[v]))
57+
}
58+
i = next[i]
59+
}
60+
}
61+
return INF
62+
}
63+
64+
fun minCost(n: Int, edges: Array<IntArray>): Int {
65+
val m = edges.size
66+
init(n, 2 * m)
67+
for (edge in edges) {
68+
val u = edge[0]
69+
val v = edge[1]
70+
val w = edge[2]
71+
add(u, v, w)
72+
add(v, u, 2 * w)
73+
}
74+
val ans = dist(0, n - 1, n)
75+
return if (ans == INF) -1 else ans
76+
}
77+
78+
companion object {
79+
private const val INF = Int.Companion.MAX_VALUE / 2 - 1
80+
}
81+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3650\. Minimum Cost Path with Edge Reversals
2+
3+
Medium
4+
5+
You are given a directed, weighted graph with `n` nodes labeled from 0 to `n - 1`, and an array `edges` where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> represents a directed edge from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code> with cost <code>w<sub>i</sub></code>.
6+
7+
Each node <code>u<sub>i</sub></code> has a switch that can be used **at most once**: when you arrive at <code>u<sub>i</sub></code> and have not yet used its switch, you may activate it on one of its incoming edges <code>v<sub>i</sub> → u<sub>i</sub></code> reverse that edge to <code>u<sub>i</sub> → v<sub>i</sub></code> and **immediately** traverse it.
8+
9+
The reversal is only valid for that single move, and using a reversed edge costs <code>2 * w<sub>i</sub></code>.
10+
11+
Return the **minimum** total cost to travel from node 0 to node `n - 1`. If it is not possible, return -1.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 4, edges = [[0,1,3],[3,1,1],[2,3,4],[0,2,2]]
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
**![](https://assets.leetcode.com/uploads/2025/05/07/e1drawio.png)**
22+
23+
* Use the path `0 → 1` (cost 3).
24+
* At node 1 reverse the original edge `3 → 1` into `1 → 3` and traverse it at cost `2 * 1 = 2`.
25+
* Total cost is `3 + 2 = 5`.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 4, edges = [[0,2,1],[2,1,1],[1,3,1],[2,3,3]]
30+
31+
**Output:** 3
32+
33+
**Explanation:**
34+
35+
* No reversal is needed. Take the path `0 → 2` (cost 1), then `2 → 1` (cost 1), then `1 → 3` (cost 1).
36+
* Total cost is `1 + 1 + 1 = 3`.
37+
38+
**Constraints:**
39+
40+
* <code>2 <= n <= 5 * 10<sup>4</sup></code>
41+
* <code>1 <= edges.length <= 10<sup>5</sup></code>
42+
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code>
43+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
44+
* <code>1 <= w<sub>i</sub> <= 1000</code>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package g3601_3700.s3651_minimum_cost_path_with_teleportations
2+
3+
// #Hard #Biweekly_Contest_163 #2025_08_17_Time_78_ms_(100.00%)_Space_45.52_MB_(97.73%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minCost(grid: Array<IntArray>, k: Int): Int {
10+
val n = grid.size
11+
val m = grid[0].size
12+
var max = -1
13+
val dp = Array<IntArray>(n) { IntArray(m) }
14+
for (i in n - 1 downTo 0) {
15+
for (j in m - 1 downTo 0) {
16+
max = max(grid[i][j], max)
17+
if (i == n - 1 && j == m - 1) {
18+
continue
19+
}
20+
if (i == n - 1) {
21+
dp[i][j] = grid[i][j + 1] + dp[i][j + 1]
22+
} else if (j == m - 1) {
23+
dp[i][j] = grid[i + 1][j] + dp[i + 1][j]
24+
} else {
25+
dp[i][j] = min(grid[i + 1][j] + dp[i + 1][j], grid[i][j + 1] + dp[i][j + 1])
26+
}
27+
}
28+
}
29+
val prev = IntArray(max + 1)
30+
prev.fill(Int.Companion.MAX_VALUE)
31+
for (i in 0..<n) {
32+
for (j in 0..<m) {
33+
prev[grid[i][j]] = min(prev[grid[i][j]], dp[i][j])
34+
}
35+
}
36+
for (i in 1..max) {
37+
prev[i] = min(prev[i], prev[i - 1])
38+
}
39+
for (tr in 1..k) {
40+
for (i in n - 1 downTo 0) {
41+
for (j in m - 1 downTo 0) {
42+
if (i == n - 1 && j == m - 1) {
43+
continue
44+
}
45+
dp[i][j] = prev[grid[i][j]]
46+
if (i == n - 1) {
47+
dp[i][j] = min(dp[i][j], grid[i][j + 1] + dp[i][j + 1])
48+
} else if (j == m - 1) {
49+
dp[i][j] = min(dp[i][j], grid[i + 1][j] + dp[i + 1][j])
50+
} else {
51+
dp[i][j] = min(dp[i][j], grid[i + 1][j] + dp[i + 1][j])
52+
dp[i][j] = min(dp[i][j], grid[i][j + 1] + dp[i][j + 1])
53+
}
54+
}
55+
}
56+
prev.fill(Int.Companion.MAX_VALUE)
57+
for (i in 0..<n) {
58+
for (j in 0..<m) {
59+
prev[grid[i][j]] = min(prev[grid[i][j]], dp[i][j])
60+
}
61+
}
62+
for (i in 1..max) {
63+
prev[i] = min(prev[i], prev[i - 1])
64+
}
65+
}
66+
return dp[0][0]
67+
}
68+
}

0 commit comments

Comments
 (0)