Skip to content

Commit 4dd8e29

Browse files
committed
Added tasks 3648-3655
1 parent 6d49514 commit 4dd8e29

File tree

9 files changed

+850
-0
lines changed
  • src/main/kotlin/g3601_3700
    • s3648_minimum_sensors_to_cover_grid
    • s3649_number_of_perfect_pairs
    • s3650_minimum_cost_path_with_edge_reversals
    • s3651_minimum_cost_path_with_teleportations
    • s3652_best_time_to_buy_and_sell_stock_using_strategy
    • s3653_xor_after_range_multiplication_queries_i
    • s3654_minimum_sum_after_divisible_sum_deletions
    • s3655_xor_after_range_multiplication_queries_ii

9 files changed

+850
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,14 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3655 |[XOR After Range Multiplication Queries II](src/main/kotlin/g3601_3700/s3655_xor_after_range_multiplication_queries_ii)| Hard | Array, Divide_and_Conquer, Weekly_Contest_463 | 26 | 100.00
2092+
| 3654 |[Minimum Sum After Divisible Sum Deletions](src/main/kotlin/g3601_3700/s3654_minimum_sum_after_divisible_sum_deletions)| Medium | Weekly_Contest_463 | 17 | 98.16
2093+
| 3653 |[XOR After Range Multiplication Queries I](src/main/kotlin/g3601_3700/s3653_xor_after_range_multiplication_queries_i)| Medium | Array, Simulation, Divide_and_Conquer, Weekly_Contest_463 | 26 | 100.00
2094+
| 3652 |[Best Time to Buy and Sell Stock using Strategy](src/main/kotlin/g3601_3700/s3652_best_time_to_buy_and_sell_stock_using_strategy)| Medium | Array, Prefix_Sum, Sliding_Window, Weekly_Contest_463 | 6 | 100.00
2095+
| 3651 |[Minimum Cost Path with Teleportations](src/main/kotlin/g3601_3700/s3651_minimum_cost_path_with_teleportations)| Hard | Biweekly_Contest_163 | 78 | 100.00
2096+
| 3650 |[Minimum Cost Path with Edge Reversals](src/main/kotlin/g3601_3700/s3650_minimum_cost_path_with_edge_reversals)| Medium | Biweekly_Contest_163 | 51 | 99.85
2097+
| 3649 |[Number of Perfect Pairs](src/main/kotlin/g3601_3700/s3649_number_of_perfect_pairs)| Medium | Biweekly_Contest_163 | 46 | 100.00
2098+
| 3648 |[Minimum Sensors to Cover Grid](src/main/kotlin/g3601_3700/s3648_minimum_sensors_to_cover_grid)| Medium | Biweekly_Contest_163 | 0 | 100.00
20912099
| 3646 |[Next Special Palindrome Number](src/main/kotlin/g3601_3700/s3646_next_special_palindrome_number)| Hard | Backtracking, Weekly_Contest_462 | 34 | 100.00
20922100
| 3645 |[Maximum Total from Optimal Activation Order](src/main/kotlin/g3601_3700/s3645_maximum_total_from_optimal_activation_order)| Medium | Array, Sorting, Greedy, Two_Pointers, Heap_Priority_Queue, Weekly_Contest_462 | 77 | 100.00
20932101
| 3644 |[Maximum K to Sort a Permutation](src/main/kotlin/g3601_3700/s3644_maximum_k_to_sort_a_permutation)| Medium | Weekly_Contest_462 | 4 | 100.00
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3648\. Minimum Sensors to Cover Grid
5+
6+
Medium
7+
8+
You are given `n × m` grid and an integer `k`.
9+
10+
A sensor placed on cell `(r, c)` covers all cells whose **Chebyshev distance** from `(r, c)` is **at most** `k`.
11+
12+
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>.
13+
14+
Your task is to return the **minimum** number of sensors required to cover every cell of the grid.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 5, m = 5, k = 1
19+
20+
**Output:** 4
21+
22+
**Explanation:**
23+
24+
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.
25+
26+
**Example 2:**
27+
28+
**Input:** n = 2, m = 2, k = 2
29+
30+
**Output:** 1
31+
32+
**Explanation:**
33+
34+
With `k = 2`, a single sensor can cover the entire `2 * 2` grid regardless of its position. Thus, the answer is 1.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= n <= 10<sup>3</sup></code>
39+
* <code>1 <= m <= 10<sup>3</sup></code>
40+
* <code>0 <= k <= 10<sup>3</sup></code>
41+
42+
## Solution
43+
44+
```kotlin
45+
class Solution {
46+
fun minSensors(n: Int, m: Int, k: Int): Int {
47+
val size = k * 2 + 1
48+
val x = n / size + (if (n % size == 0) 0 else 1)
49+
val y = m / size + (if (m % size == 0) 0 else 1)
50+
return x * y
51+
}
52+
}
53+
```
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3649\. Number of Perfect Pairs
5+
6+
Medium
7+
8+
You are given an integer array `nums`.
9+
10+
A pair of indices `(i, j)` is called **perfect** if the following conditions are satisfied:
11+
12+
* `i < j`
13+
* Let `a = nums[i]`, `b = nums[j]`. Then:
14+
* `min(|a - b|, |a + b|) <= min(|a|, |b|)`
15+
* `max(|a - b|, |a + b|) >= max(|a|, |b|)`
16+
17+
Return the number of **distinct** perfect pairs.
18+
19+
**Note:** The absolute value `|x|` refers to the **non-negative** value of `x`.
20+
21+
**Example 1:**
22+
23+
**Input:** nums = [0,1,2,3]
24+
25+
**Output:** 2
26+
27+
**Explanation:**
28+
29+
There are 2 perfect pairs:
30+
31+
| `(i, j)` | `(a, b)` | `min(|a − b|, |a + b|)` | `min(|a|, |b|)` | `max(|a − b|, |a + b|)` | `max(|a|, |b|)` |
32+
|----------|-----------|-------------------------------------|-----------------|-------------------------------------|-----------------|
33+
| (1, 2) | (1, 2) | `min(|1 − 2|, |1 + 2|) = 1` | 1 | `max(|1 − 2|, |1 + 2|) = 3` | 2 |
34+
| (2, 3) | (2, 3) | `min(|2 − 3|, |2 + 3|) = 1` | 2 | `max(|2 − 3|, |2 + 3|) = 5` | 3 |
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [-3,2,-1,4]
39+
40+
**Output:** 4
41+
42+
**Explanation:**
43+
44+
There are 4 perfect pairs:
45+
46+
| `(i, j)` | `(a, b)` | `min(|a − b|, |a + b|)` | `min(|a|, |b|)` | `max(|a − b|, |a + b|)` | `max(|a|, |b|)` |
47+
|----------|-----------|-----------------------------------------------|-----------------|-----------------------------------------------|-----------------|
48+
| (0, 1) | (-3, 2) | `min(|-3 - 2|, |-3 + 2|) = 1` | 2 | `max(|-3 - 2|, |-3 + 2|) = 5` | 3 |
49+
| (0, 3) | (-3, 4) | `min(|-3 - 4|, |-3 + 4|) = 1` | 3 | `max(|-3 - 4|, |-3 + 4|) = 7` | 4 |
50+
| (1, 2) | (2, -1) | `min(|2 - (-1)|, |2 + (-1)|) = 1` | 1 | `max(|2 - (-1)|, |2 + (-1)|) = 3` | 2 |
51+
| (1, 3) | (2, 4) | `min(|2 - 4|, |2 + 4|) = 2` | 2 | `max(|2 - 4|, |2 + 4|) = 6` | 4 |
52+
53+
**Example 3:**
54+
55+
**Input:** nums = [1,10,100,1000]
56+
57+
**Output:** 0
58+
59+
**Explanation:**
60+
61+
There are no perfect pairs. Thus, the answer is 0.
62+
63+
**Constraints:**
64+
65+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
66+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
67+
68+
## Solution
69+
70+
```kotlin
71+
import kotlin.math.abs
72+
73+
class Solution {
74+
fun perfectPairs(nums: IntArray): Long {
75+
val n = nums.size
76+
val arr = LongArray(n)
77+
for (i in 0..<n) {
78+
arr[i] = abs(nums[i].toLong())
79+
}
80+
arr.sort()
81+
var cnt: Long = 0
82+
var r = 0
83+
for (i in 0..<n) {
84+
if (r < i) {
85+
r = i
86+
}
87+
while (r + 1 < n && arr[r + 1] <= 2 * arr[i]) {
88+
r++
89+
}
90+
cnt += (r - i).toLong()
91+
}
92+
return cnt
93+
}
94+
}
95+
```
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3650\. Minimum Cost Path with Edge Reversals
5+
6+
Medium
7+
8+
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>.
9+
10+
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.
11+
12+
The reversal is only valid for that single move, and using a reversed edge costs <code>2 * w<sub>i</sub></code>.
13+
14+
Return the **minimum** total cost to travel from node 0 to node `n - 1`. If it is not possible, return -1.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 4, edges = \[\[0,1,3],[3,1,1],[2,3,4],[0,2,2]]
19+
20+
**Output:** 5
21+
22+
**Explanation:**
23+
24+
**![](https://assets.leetcode.com/uploads/2025/05/07/e1drawio.png)**
25+
26+
* Use the path `0 → 1` (cost 3).
27+
* At node 1 reverse the original edge `3 → 1` into `1 → 3` and traverse it at cost `2 * 1 = 2`.
28+
* Total cost is `3 + 2 = 5`.
29+
30+
**Example 2:**
31+
32+
**Input:** n = 4, edges = \[\[0,2,1],[2,1,1],[1,3,1],[2,3,3]]
33+
34+
**Output:** 3
35+
36+
**Explanation:**
37+
38+
* No reversal is needed. Take the path `0 → 2` (cost 1), then `2 → 1` (cost 1), then `1 → 3` (cost 1).
39+
* Total cost is `1 + 1 + 1 = 3`.
40+
41+
**Constraints:**
42+
43+
* <code>2 <= n <= 5 * 10<sup>4</sup></code>
44+
* <code>1 <= edges.length <= 10<sup>5</sup></code>
45+
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code>
46+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
47+
* <code>1 <= w<sub>i</sub> <= 1000</code>
48+
49+
## Solution
50+
51+
```kotlin
52+
import java.util.PriorityQueue
53+
54+
class Solution {
55+
private var cnt = 0
56+
private lateinit var head: IntArray
57+
private lateinit var next: IntArray
58+
private lateinit var to: IntArray
59+
private lateinit var weight: IntArray
60+
61+
private class Dist(var u: Int, var d: Int) : Comparable<Dist> {
62+
override fun compareTo(other: Dist): Int {
63+
return d.toLong().compareTo(other.d.toLong())
64+
}
65+
}
66+
67+
private fun init(n: Int, m: Int) {
68+
head = IntArray(n)
69+
head.fill(-1)
70+
next = IntArray(m)
71+
to = IntArray(m)
72+
weight = IntArray(m)
73+
}
74+
75+
private fun add(u: Int, v: Int, w: Int) {
76+
to[cnt] = v
77+
weight[cnt] = w
78+
next[cnt] = head[u]
79+
head[u] = cnt++
80+
}
81+
82+
private fun dist(s: Int, t: Int, n: Int): Int {
83+
val queue: PriorityQueue<Dist> = PriorityQueue<Dist>()
84+
val dist = IntArray(n)
85+
dist.fill(INF)
86+
dist[s] = 0
87+
queue.add(Dist(s, dist[s]))
88+
while (queue.isNotEmpty()) {
89+
val d = queue.remove()
90+
val u = d.u
91+
if (dist[u] < d.d) {
92+
continue
93+
}
94+
if (u == t) {
95+
return dist[t]
96+
}
97+
var i = head[u]
98+
while (i != -1) {
99+
val v = to[i]
100+
val w = weight[i]
101+
if (dist[v] > dist[u] + w) {
102+
dist[v] = dist[u] + w
103+
queue.add(Dist(v, dist[v]))
104+
}
105+
i = next[i]
106+
}
107+
}
108+
return INF
109+
}
110+
111+
fun minCost(n: Int, edges: Array<IntArray>): Int {
112+
val m = edges.size
113+
init(n, 2 * m)
114+
for (edge in edges) {
115+
val u = edge[0]
116+
val v = edge[1]
117+
val w = edge[2]
118+
add(u, v, w)
119+
add(v, u, 2 * w)
120+
}
121+
val ans = dist(0, n - 1, n)
122+
return if (ans == INF) -1 else ans
123+
}
124+
125+
companion object {
126+
private const val INF = Int.Companion.MAX_VALUE / 2 - 1
127+
}
128+
}
129+
```

0 commit comments

Comments
 (0)