Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package g3601_3700.s3648_minimum_sensors_to_cover_grid

// #Medium #Biweekly_Contest_163 #2025_08_17_Time_0_ms_(100.00%)_Space_41.03_MB_(100.00%)

class Solution {
fun minSensors(n: Int, m: Int, k: Int): Int {
val size = k * 2 + 1
val x = n / size + (if (n % size == 0) 0 else 1)
val y = m / size + (if (m % size == 0) 0 else 1)
return x * y
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
3648\. Minimum Sensors to Cover Grid

Medium

You are given `n × m` grid and an integer `k`.

A sensor placed on cell `(r, c)` covers all cells whose **Chebyshev distance** from `(r, c)` is **at most** `k`.

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>.

Your task is to return the **minimum** number of sensors required to cover every cell of the grid.

**Example 1:**

**Input:** n = 5, m = 5, k = 1

**Output:** 4

**Explanation:**

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.

**Example 2:**

**Input:** n = 2, m = 2, k = 2

**Output:** 1

**Explanation:**

With `k = 2`, a single sensor can cover the entire `2 * 2` grid regardless of its position. Thus, the answer is 1.

**Constraints:**

* <code>1 <= n <= 10<sup>3</sup></code>
* <code>1 <= m <= 10<sup>3</sup></code>
* <code>0 <= k <= 10<sup>3</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3601_3700.s3649_number_of_perfect_pairs

// #Medium #Biweekly_Contest_163 #2025_08_17_Time_46_ms_(100.00%)_Space_60.00_MB_(100.00%)

import kotlin.math.abs

class Solution {
fun perfectPairs(nums: IntArray): Long {
val n = nums.size
val arr = LongArray(n)
for (i in 0..<n) {
arr[i] = abs(nums[i].toLong())
}
arr.sort()
var cnt: Long = 0
var r = 0
for (i in 0..<n) {
if (r < i) {
r = i
}
while (r + 1 < n && arr[r + 1] <= 2 * arr[i]) {
r++
}
cnt += (r - i).toLong()
}
return cnt
}
}
63 changes: 63 additions & 0 deletions src/main/kotlin/g3601_3700/s3649_number_of_perfect_pairs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
3649\. Number of Perfect Pairs

Medium

You are given an integer array `nums`.

A pair of indices `(i, j)` is called **perfect** if the following conditions are satisfied:

* `i < j`
* Let `a = nums[i]`, `b = nums[j]`. Then:
* `min(|a - b|, |a + b|) <= min(|a|, |b|)`
* `max(|a - b|, |a + b|) >= max(|a|, |b|)`

Return the number of **distinct** perfect pairs.

**Note:** The absolute value `|x|` refers to the **non-negative** value of `x`.

**Example 1:**

**Input:** nums = [0,1,2,3]

**Output:** 2

**Explanation:**

There are 2 perfect pairs:

| `(i, j)` | `(a, b)` | `min(|a − b|, |a + b|)` | `min(|a|, |b|)` | `max(|a − b|, |a + b|)` | `max(|a|, |b|)` |
|----------|-----------|-------------------------------------|-----------------|-------------------------------------|-----------------|
| (1, 2) | (1, 2) | `min(|1 − 2|, |1 + 2|) = 1` | 1 | `max(|1 − 2|, |1 + 2|) = 3` | 2 |
| (2, 3) | (2, 3) | `min(|2 − 3|, |2 + 3|) = 1` | 2 | `max(|2 − 3|, |2 + 3|) = 5` | 3 |

**Example 2:**

**Input:** nums = [-3,2,-1,4]

**Output:** 4

**Explanation:**

There are 4 perfect pairs:

| `(i, j)` | `(a, b)` | `min(|a − b|, |a + b|)` | `min(|a|, |b|)` | `max(|a − b|, |a + b|)` | `max(|a|, |b|)` |
|----------|-----------|-----------------------------------------------|-----------------|-----------------------------------------------|-----------------|
| (0, 1) | (-3, 2) | `min(|-3 - 2|, |-3 + 2|) = 1` | 2 | `max(|-3 - 2|, |-3 + 2|) = 5` | 3 |
| (0, 3) | (-3, 4) | `min(|-3 - 4|, |-3 + 4|) = 1` | 3 | `max(|-3 - 4|, |-3 + 4|) = 7` | 4 |
| (1, 2) | (2, -1) | `min(|2 - (-1)|, |2 + (-1)|) = 1` | 1 | `max(|2 - (-1)|, |2 + (-1)|) = 3` | 2 |
| (1, 3) | (2, 4) | `min(|2 - 4|, |2 + 4|) = 2` | 2 | `max(|2 - 4|, |2 + 4|) = 6` | 4 |

**Example 3:**

**Input:** nums = [1,10,100,1000]

**Output:** 0

**Explanation:**

There are no perfect pairs. Thus, the answer is 0.

**Constraints:**

* <code>2 <= nums.length <= 10<sup>5</sup></code>
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package g3601_3700.s3650_minimum_cost_path_with_edge_reversals

// #Medium #Biweekly_Contest_163 #2025_08_17_Time_51_ms_(99.85%)_Space_110.03_MB_(49.54%)

import java.util.PriorityQueue

class Solution {
private var cnt = 0
private lateinit var head: IntArray
private lateinit var next: IntArray
private lateinit var to: IntArray
private lateinit var weight: IntArray

private class Dist(var u: Int, var d: Int) : Comparable<Dist> {
override fun compareTo(other: Dist): Int {
return d.toLong().compareTo(other.d.toLong())
}
}

private fun init(n: Int, m: Int) {
head = IntArray(n)
head.fill(-1)
next = IntArray(m)
to = IntArray(m)
weight = IntArray(m)
}

private fun add(u: Int, v: Int, w: Int) {
to[cnt] = v
weight[cnt] = w
next[cnt] = head[u]
head[u] = cnt++
}

private fun dist(s: Int, t: Int, n: Int): Int {
val queue: PriorityQueue<Dist> = PriorityQueue<Dist>()
val dist = IntArray(n)
dist.fill(INF)
dist[s] = 0
queue.add(Dist(s, dist[s]))
while (queue.isNotEmpty()) {
val d = queue.remove()
val u = d.u
if (dist[u] < d.d) {
continue
}
if (u == t) {
return dist[t]
}
var i = head[u]
while (i != -1) {
val v = to[i]
val w = weight[i]
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w
queue.add(Dist(v, dist[v]))
}
i = next[i]
}
}
return INF
}

fun minCost(n: Int, edges: Array<IntArray>): Int {
val m = edges.size
init(n, 2 * m)
for (edge in edges) {
val u = edge[0]
val v = edge[1]
val w = edge[2]
add(u, v, w)
add(v, u, 2 * w)
}
val ans = dist(0, n - 1, n)
return if (ans == INF) -1 else ans
}

companion object {
private const val INF = Int.Companion.MAX_VALUE / 2 - 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
3650\. Minimum Cost Path with Edge Reversals

Medium

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>.

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.

The reversal is only valid for that single move, and using a reversed edge costs <code>2 * w<sub>i</sub></code>.

Return the **minimum** total cost to travel from node 0 to node `n - 1`. If it is not possible, return -1.

**Example 1:**

**Input:** n = 4, edges = [[0,1,3],[3,1,1],[2,3,4],[0,2,2]]

**Output:** 5

**Explanation:**

**![](https://assets.leetcode.com/uploads/2025/05/07/e1drawio.png)**

* Use the path `0 → 1` (cost 3).
* At node 1 reverse the original edge `3 → 1` into `1 → 3` and traverse it at cost `2 * 1 = 2`.
* Total cost is `3 + 2 = 5`.

**Example 2:**

**Input:** n = 4, edges = [[0,2,1],[2,1,1],[1,3,1],[2,3,3]]

**Output:** 3

**Explanation:**

* No reversal is needed. Take the path `0 → 2` (cost 1), then `2 → 1` (cost 1), then `1 → 3` (cost 1).
* Total cost is `1 + 1 + 1 = 3`.

**Constraints:**

* <code>2 <= n <= 5 * 10<sup>4</sup></code>
* <code>1 <= edges.length <= 10<sup>5</sup></code>
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code>
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
* <code>1 <= w<sub>i</sub> <= 1000</code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package g3601_3700.s3651_minimum_cost_path_with_teleportations

// #Hard #Biweekly_Contest_163 #2025_08_17_Time_78_ms_(100.00%)_Space_45.52_MB_(97.73%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun minCost(grid: Array<IntArray>, k: Int): Int {
val n = grid.size
val m = grid[0].size
var max = -1
val dp = Array<IntArray>(n) { IntArray(m) }
for (i in n - 1 downTo 0) {
for (j in m - 1 downTo 0) {
max = max(grid[i][j], max)
if (i == n - 1 && j == m - 1) {
continue
}
if (i == n - 1) {
dp[i][j] = grid[i][j + 1] + dp[i][j + 1]
} else if (j == m - 1) {
dp[i][j] = grid[i + 1][j] + dp[i + 1][j]
} else {
dp[i][j] = min(grid[i + 1][j] + dp[i + 1][j], grid[i][j + 1] + dp[i][j + 1])
}
}
}
val prev = IntArray(max + 1)
prev.fill(Int.Companion.MAX_VALUE)
for (i in 0..<n) {
for (j in 0..<m) {
prev[grid[i][j]] = min(prev[grid[i][j]], dp[i][j])
}
}
for (i in 1..max) {
prev[i] = min(prev[i], prev[i - 1])
}
for (tr in 1..k) {
for (i in n - 1 downTo 0) {
for (j in m - 1 downTo 0) {
if (i == n - 1 && j == m - 1) {
continue
}
dp[i][j] = prev[grid[i][j]]
if (i == n - 1) {
dp[i][j] = min(dp[i][j], grid[i][j + 1] + dp[i][j + 1])
} else if (j == m - 1) {
dp[i][j] = min(dp[i][j], grid[i + 1][j] + dp[i + 1][j])
} else {
dp[i][j] = min(dp[i][j], grid[i + 1][j] + dp[i + 1][j])
dp[i][j] = min(dp[i][j], grid[i][j + 1] + dp[i][j + 1])
}
}
}
prev.fill(Int.Companion.MAX_VALUE)
for (i in 0..<n) {
for (j in 0..<m) {
prev[grid[i][j]] = min(prev[grid[i][j]], dp[i][j])
}
}
for (i in 1..max) {
prev[i] = min(prev[i], prev[i - 1])
}
}
return dp[0][0]
}
}
Loading
Loading