Skip to content

Commit ad89705

Browse files
authored
Added tasks 3648-3655
1 parent 0ec11ba commit ad89705

File tree

25 files changed

+1078
-1
lines changed

25 files changed

+1078
-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+
public class Solution {
6+
public int minSensors(int n, int m, int k) {
7+
int size = k * 2 + 1;
8+
int x = n / size + (n % size == 0 ? 0 : 1);
9+
int y = m / size + (m % size == 0 ? 0 : 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 java.util.Arrays;
6+
7+
public class Solution {
8+
public long perfectPairs(int[] nums) {
9+
int n = nums.length;
10+
long[] arr = new long[n];
11+
for (int i = 0; i < n; i++) {
12+
arr[i] = Math.abs((long) nums[i]);
13+
}
14+
Arrays.sort(arr);
15+
long cnt = 0;
16+
int r = 0;
17+
for (int i = 0; i < n; i++) {
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);
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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.Arrays;
6+
import java.util.PriorityQueue;
7+
8+
@SuppressWarnings({"java:S1210", "java:S2234"})
9+
public class Solution {
10+
private static final int INF = Integer.MAX_VALUE / 2 - 1;
11+
private int cnt;
12+
private int[] head;
13+
private int[] next;
14+
private int[] to;
15+
private int[] weight;
16+
17+
private static class Dist implements Comparable<Dist> {
18+
int u;
19+
int d;
20+
21+
public Dist(int u, int d) {
22+
this.u = u;
23+
this.d = d;
24+
}
25+
26+
@Override
27+
public int compareTo(Dist o) {
28+
return Long.compare(d, o.d);
29+
}
30+
}
31+
32+
private void init(int n, int m) {
33+
head = new int[n];
34+
Arrays.fill(head, -1);
35+
next = new int[m];
36+
to = new int[m];
37+
weight = new int[m];
38+
}
39+
40+
private void add(int u, int v, int w) {
41+
to[cnt] = v;
42+
weight[cnt] = w;
43+
next[cnt] = head[u];
44+
head[u] = cnt++;
45+
}
46+
47+
private int dist(int s, int t, int n) {
48+
PriorityQueue<Dist> queue = new PriorityQueue<>();
49+
int[] dist = new int[n];
50+
Arrays.fill(dist, INF);
51+
dist[s] = 0;
52+
queue.add(new Dist(s, dist[s]));
53+
while (!queue.isEmpty()) {
54+
Dist d = queue.remove();
55+
int u = d.u;
56+
if (dist[u] < d.d) {
57+
continue;
58+
}
59+
if (u == t) {
60+
return dist[t];
61+
}
62+
for (int i = head[u]; i != -1; i = next[i]) {
63+
int v = to[i];
64+
int w = weight[i];
65+
if (dist[v] > dist[u] + w) {
66+
dist[v] = dist[u] + w;
67+
queue.add(new Dist(v, dist[v]));
68+
}
69+
}
70+
}
71+
return INF;
72+
}
73+
74+
public int minCost(int n, int[][] edges) {
75+
int m = edges.length;
76+
init(n, 2 * m);
77+
for (int[] edge : edges) {
78+
int u = edge[0];
79+
int v = edge[1];
80+
int w = edge[2];
81+
add(u, v, w);
82+
add(v, u, 2 * w);
83+
}
84+
int ans = dist(0, n - 1, n);
85+
return ans == INF ? -1 : ans;
86+
}
87+
}
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 java.util.Arrays;
6+
7+
public class Solution {
8+
public int minCost(int[][] grid, int k) {
9+
int n = grid.length;
10+
int m = grid[0].length;
11+
int max = -1;
12+
int[][] dp = new int[n][m];
13+
for (int i = n - 1; i >= 0; i--) {
14+
for (int j = m - 1; j >= 0; j--) {
15+
max = Math.max(grid[i][j], max);
16+
if (i == n - 1 && j == m - 1) {
17+
continue;
18+
}
19+
if (i == n - 1) {
20+
dp[i][j] = grid[i][j + 1] + dp[i][j + 1];
21+
} else if (j == m - 1) {
22+
dp[i][j] = grid[i + 1][j] + dp[i + 1][j];
23+
} else {
24+
dp[i][j] =
25+
Math.min(grid[i + 1][j] + dp[i + 1][j], grid[i][j + 1] + dp[i][j + 1]);
26+
}
27+
}
28+
}
29+
int[] prev = new int[max + 1];
30+
Arrays.fill(prev, Integer.MAX_VALUE);
31+
for (int i = 0; i < n; i++) {
32+
for (int j = 0; j < m; j++) {
33+
prev[grid[i][j]] = Math.min(prev[grid[i][j]], dp[i][j]);
34+
}
35+
}
36+
// int currcost = prev[0];
37+
for (int i = 1; i <= max; i++) {
38+
prev[i] = Math.min(prev[i], prev[i - 1]);
39+
}
40+
for (int tr = 1; tr <= k; tr++) {
41+
for (int i = n - 1; i >= 0; i--) {
42+
for (int j = m - 1; j >= 0; j--) {
43+
if (i == n - 1 && j == m - 1) {
44+
continue;
45+
}
46+
dp[i][j] = prev[grid[i][j]];
47+
if (i == n - 1) {
48+
dp[i][j] = Math.min(dp[i][j], grid[i][j + 1] + dp[i][j + 1]);
49+
} else if (j == m - 1) {
50+
dp[i][j] = Math.min(dp[i][j], grid[i + 1][j] + dp[i + 1][j]);
51+
} else {
52+
dp[i][j] = Math.min(dp[i][j], grid[i + 1][j] + dp[i + 1][j]);
53+
dp[i][j] = Math.min(dp[i][j], grid[i][j + 1] + dp[i][j + 1]);
54+
}
55+
}
56+
}
57+
Arrays.fill(prev, Integer.MAX_VALUE);
58+
for (int i = 0; i < n; i++) {
59+
for (int j = 0; j < m; j++) {
60+
prev[grid[i][j]] = Math.min(prev[grid[i][j]], dp[i][j]);
61+
}
62+
}
63+
// int currcost = prev[0];
64+
for (int i = 1; i <= max; i++) {
65+
prev[i] = Math.min(prev[i], prev[i - 1]);
66+
}
67+
}
68+
return dp[0][0];
69+
}
70+
}

0 commit comments

Comments
 (0)