Skip to content

Commit 01aa41c

Browse files
committed
Added tasks 3566-3569
1 parent dd4e291 commit 01aa41c

File tree

12 files changed

+605
-0
lines changed

12 files changed

+605
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3501_3600.s3566_partition_array_into_two_equal_product_subsets;
2+
3+
// #Medium #2025_06_01_Time_0_ms_(100.00%)_Space_42.01_MB_(78.42%)
4+
5+
public class Solution {
6+
public boolean checkEqualPartitions(int[] nums, long target) {
7+
for (int num : nums) {
8+
if (target % (long) num != 0) {
9+
return false;
10+
}
11+
}
12+
long pro = 1;
13+
for (long n : nums) {
14+
pro *= n;
15+
}
16+
return pro == target * target;
17+
}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3566\. Partition Array into Two Equal Product Subsets
2+
3+
Medium
4+
5+
You are given an integer array `nums` containing **distinct** positive integers and an integer `target`.
6+
7+
Determine if you can partition `nums` into two **non-empty** **disjoint** **subsets**, with each element belonging to **exactly one** subset, such that the product of the elements in each subset is equal to `target`.
8+
9+
Return `true` if such a partition exists and `false` otherwise.
10+
11+
A **subset** of an array is a selection of elements of the array.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [3,1,6,8,4], target = 24
16+
17+
**Output:** true
18+
19+
**Explanation:** The subsets `[3, 8]` and `[1, 6, 4]` each have a product of 24. Hence, the output is true.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [2,5,3,7], target = 15
24+
25+
**Output:** false
26+
27+
**Explanation:** There is no way to partition `nums` into two non-empty disjoint subsets such that both subsets have a product of 15. Hence, the output is false.
28+
29+
**Constraints:**
30+
31+
* `3 <= nums.length <= 12`
32+
* <code>1 <= target <= 10<sup>15</sup></code>
33+
* `1 <= nums[i] <= 100`
34+
* All elements of `nums` are **distinct**.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3501_3600.s3567_minimum_absolute_difference_in_sliding_submatrix;
2+
3+
// #Medium #2025_06_01_Time_7_ms_(99.65%)_Space_46.08_MB_(10.21%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int[][] minAbsDiff(int[][] grid, int k) {
9+
int rows = grid.length;
10+
int cols = grid[0].length;
11+
int[][] result = new int[rows - k + 1][cols - k + 1];
12+
for (int x = 0; x <= rows - k; x++) {
13+
for (int y = 0; y <= cols - k; y++) {
14+
int size = k * k;
15+
int[] elements = new int[size];
16+
int idx = 0;
17+
for (int i = x; i < x + k; i++) {
18+
for (int j = y; j < y + k; j++) {
19+
elements[idx++] = grid[i][j];
20+
}
21+
}
22+
Arrays.sort(elements);
23+
int minDiff = Integer.MAX_VALUE;
24+
for (int i = 1; i < size; i++) {
25+
if (elements[i] != elements[i - 1]) {
26+
minDiff = Math.min(minDiff, elements[i] - elements[i - 1]);
27+
if (minDiff == 1) break;
28+
}
29+
}
30+
result[x][y] = minDiff == Integer.MAX_VALUE ? 0 : minDiff;
31+
}
32+
}
33+
return result;
34+
}
35+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
3567\. Minimum Absolute Difference in Sliding Submatrix
2+
3+
Medium
4+
5+
You are given an `m x n` integer matrix `grid` and an integer `k`.
6+
7+
For every contiguous `k x k` **submatrix** of `grid`, compute the **minimum absolute** difference between any two **distinct** values within that **submatrix**.
8+
9+
Return a 2D array `ans` of size `(m - k + 1) x (n - k + 1)`, where `ans[i][j]` is the minimum absolute difference in the submatrix whose top-left corner is `(i, j)` in `grid`.
10+
11+
**Note**: If all elements in the submatrix have the same value, the answer will be 0.
12+
13+
A submatrix `(x1, y1, x2, y2)` is a matrix that is formed by choosing all cells `matrix[x][y]` where `x1 <= x <= x2` and `y1 <= y <= y2`.
14+
15+
**Example 1:**
16+
17+
**Input:** grid = [[1,8],[3,-2]], k = 2
18+
19+
**Output:** [[2]]
20+
21+
**Explanation:**
22+
23+
* There is only one possible `k x k` submatrix: `[[1, 8], [3, -2]]`.
24+
* Distinct values in the submatrix are `[1, 8, 3, -2]`.
25+
* The minimum absolute difference in the submatrix is `|1 - 3| = 2`. Thus, the answer is `[[2]]`.
26+
27+
**Example 2:**
28+
29+
**Input:** grid = [[3,-1]], k = 1
30+
31+
**Output:** [[0,0]]
32+
33+
**Explanation:**
34+
35+
* Both `k x k` submatrix has only one distinct element.
36+
* Thus, the answer is `[[0, 0]]`.
37+
38+
**Example 3:**
39+
40+
**Input:** grid = [[1,-2,3],[2,3,5]], k = 2
41+
42+
**Output:** [[1,2]]
43+
44+
**Explanation:**
45+
46+
* There are two possible `k × k` submatrix:
47+
* Starting at `(0, 0)`: `[[1, -2], [2, 3]]`.
48+
* Distinct values in the submatrix are `[1, -2, 2, 3]`.
49+
* The minimum absolute difference in the submatrix is `|1 - 2| = 1`.
50+
* Starting at `(0, 1)`: `[[-2, 3], [3, 5]]`.
51+
* Distinct values in the submatrix are `[-2, 3, 5]`.
52+
* The minimum absolute difference in the submatrix is `|3 - 5| = 2`.
53+
* Thus, the answer is `[[1, 2]]`.
54+
55+
**Constraints:**
56+
57+
* `1 <= m == grid.length <= 30`
58+
* `1 <= n == grid[i].length <= 30`
59+
* <code>-10<sup>5</sup> <= grid[i][j] <= 10<sup>5</sup></code>
60+
* `1 <= k <= min(m, n)`
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package g3501_3600.s3568_minimum_moves_to_clean_the_classroom;
2+
3+
// #Medium #2025_06_01_Time_90_ms_(100.00%)_Space_53.82_MB_(99.66%)
4+
5+
import java.util.ArrayDeque;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.Queue;
10+
11+
public class Solution {
12+
private static class State {
13+
int x;
14+
int y;
15+
int energy;
16+
int mask;
17+
int steps;
18+
19+
State(int x, int y, int energy, int mask, int steps) {
20+
this.x = x;
21+
this.y = y;
22+
this.energy = energy;
23+
this.mask = mask;
24+
this.steps = steps;
25+
}
26+
}
27+
28+
public int minMoves(String[] classroom, int energy) {
29+
int m = classroom.length, n = classroom[0].length();
30+
char[][] grid = new char[m][n];
31+
for (int i = 0; i < m; i++) {
32+
grid[i] = classroom[i].toCharArray();
33+
}
34+
int startX = -1, startY = -1;
35+
List<int[]> lumetarkon = new ArrayList<>();
36+
for (int i = 0; i < m; i++) {
37+
for (int j = 0; j < n; j++) {
38+
char c = grid[i][j];
39+
if (c == 'S') {
40+
startX = i;
41+
startY = j;
42+
} else if (c == 'L') {
43+
lumetarkon.add(new int[] {i, j});
44+
}
45+
}
46+
}
47+
int totalLitter = lumetarkon.size();
48+
int allMask = (1 << totalLitter) - 1;
49+
int[][][] visited = new int[m][n][1 << totalLitter];
50+
for (int[][] layer : visited) {
51+
for (int[] row : layer) {
52+
Arrays.fill(row, -1);
53+
}
54+
}
55+
Queue<State> queue = new ArrayDeque<>();
56+
queue.offer(new State(startX, startY, energy, 0, 0));
57+
visited[startX][startY][0] = energy;
58+
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
59+
while (!queue.isEmpty()) {
60+
State curr = queue.poll();
61+
if (curr.mask == allMask) {
62+
return curr.steps;
63+
}
64+
for (int[] dir : dirs) {
65+
int nx = curr.x + dir[0];
66+
int ny = curr.y + dir[1];
67+
if (nx < 0 || ny < 0 || nx >= m || ny >= n || grid[nx][ny] == 'X') {
68+
continue;
69+
}
70+
int nextEnergy = curr.energy - 1;
71+
if (nextEnergy < 0) {
72+
continue;
73+
}
74+
char cell = grid[nx][ny];
75+
if (cell == 'R') {
76+
nextEnergy = energy;
77+
}
78+
int nextMask = curr.mask;
79+
if (cell == 'L') {
80+
for (int i = 0; i < lumetarkon.size(); i++) {
81+
int[] pos = lumetarkon.get(i);
82+
if (pos[0] == nx && pos[1] == ny) {
83+
nextMask |= (1 << i);
84+
break;
85+
}
86+
}
87+
}
88+
if (visited[nx][ny][nextMask] < nextEnergy) {
89+
visited[nx][ny][nextMask] = nextEnergy;
90+
queue.offer(new State(nx, ny, nextEnergy, nextMask, curr.steps + 1));
91+
}
92+
}
93+
}
94+
return -1;
95+
}
96+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
3568\. Minimum Moves to Clean the Classroom
2+
3+
Medium
4+
5+
You are given an `m x n` grid `classroom` where a student volunteer is tasked with cleaning up litter scattered around the room. Each cell in the grid is one of the following:
6+
7+
* `'S'`: Starting position of the student
8+
* `'L'`: Litter that must be collected (once collected, the cell becomes empty)
9+
* `'R'`: Reset area that restores the student's energy to full capacity, regardless of their current energy level (can be used multiple times)
10+
* `'X'`: Obstacle the student cannot pass through
11+
* `'.'`: Empty space
12+
13+
You are also given an integer `energy`, representing the student's maximum energy capacity. The student starts with this energy from the starting position `'S'`.
14+
15+
Each move to an adjacent cell (up, down, left, or right) costs 1 unit of energy. If the energy reaches 0, the student can only continue if they are on a reset area `'R'`, which resets the energy to its **maximum** capacity `energy`.
16+
17+
Return the **minimum** number of moves required to collect all litter items, or `-1` if it's impossible.
18+
19+
**Example 1:**
20+
21+
**Input:** classroom = ["S.", "XL"], energy = 2
22+
23+
**Output:** 2
24+
25+
**Explanation:**
26+
27+
* The student starts at cell `(0, 0)` with 2 units of energy.
28+
* Since cell `(1, 0)` contains an obstacle 'X', the student cannot move directly downward.
29+
* A valid sequence of moves to collect all litter is as follows:
30+
* Move 1: From `(0, 0)``(0, 1)` with 1 unit of energy and 1 unit remaining.
31+
* Move 2: From `(0, 1)``(1, 1)` to collect the litter `'L'`.
32+
* The student collects all the litter using 2 moves. Thus, the output is 2.
33+
34+
**Example 2:**
35+
36+
**Input:** classroom = ["LS", "RL"], energy = 4
37+
38+
**Output:** 3
39+
40+
**Explanation:**
41+
42+
* The student starts at cell `(0, 1)` with 4 units of energy.
43+
* A valid sequence of moves to collect all litter is as follows:
44+
* Move 1: From `(0, 1)``(0, 0)` to collect the first litter `'L'` with 1 unit of energy used and 3 units remaining.
45+
* Move 2: From `(0, 0)``(1, 0)` to `'R'` to reset and restore energy back to 4.
46+
* Move 3: From `(1, 0)``(1, 1)` to collect the second litter `'L'`.
47+
* The student collects all the litter using 3 moves. Thus, the output is 3.
48+
49+
**Example 3:**
50+
51+
**Input:** classroom = ["L.S", "RXL"], energy = 3
52+
53+
**Output:** \-1
54+
55+
**Explanation:**
56+
57+
No valid path collects all `'L'`.
58+
59+
**Constraints:**
60+
61+
* `1 <= m == classroom.length <= 20`
62+
* `1 <= n == classroom[i].length <= 20`
63+
* `classroom[i][j]` is one of `'S'`, `'L'`, `'R'`, `'X'`, or `'.'`
64+
* `1 <= energy <= 50`
65+
* There is exactly **one** `'S'` in the grid.
66+
* There are **at most** 10 `'L'` cells in the grid.

0 commit comments

Comments
 (0)