Skip to content

Commit d50384d

Browse files
authored
Added tasks 3566-3569
1 parent 2ee3575 commit d50384d

File tree

17 files changed

+631
-5
lines changed

17 files changed

+631
-5
lines changed

src/main/java/g0101_0200/s0178_rank_scores/script.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ FROM
77
Scores
88
ORDER BY
99
Rank ASC;
10-

src/main/java/g0101_0200/s0182_duplicate_emails/script.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ GROUP BY
88
Email
99
HAVING
1010
COUNT(Email) > 1;
11-

src/main/java/g3501_3600/s3558_number_of_ways_to_assign_edge_weights_i/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3501_3600.s3558_number_of_ways_to_assign_edge_weights_i;
22

3-
// #Medium #Math #Tree #Depth_First_Search #2025_05_27_Time_12_ms_(100.00%)_Space_106.62_MB_(76.01%)
3+
// #Medium #Math #Depth_First_Search #Tree #2025_05_27_Time_12_ms_(100.00%)_Space_106.62_MB_(76.01%)
44

55
public class Solution {
66
private static int mod = (int) 1e9 + 7;

src/main/java/g3501_3600/s3559_number_of_ways_to_assign_edge_weights_ii/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3501_3600.s3559_number_of_ways_to_assign_edge_weights_ii;
22

3-
// #Hard #Array #Dynamic_Programming #Math #Tree #Depth_First_Search
3+
// #Hard #Array #Dynamic_Programming #Math #Depth_First_Search #Tree
44
// #2025_05_27_Time_138_ms_(64.66%)_Space_133.20_MB_(11.56%)
55

66
import java.util.ArrayList;

src/main/java/g3501_3600/s3562_maximum_profit_from_trading_stocks_with_discounts/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3501_3600.s3562_maximum_profit_from_trading_stocks_with_discounts;
22

3-
// #Hard #Array #Dynamic_Programming #Tree #Depth_First_Search
3+
// #Hard #Array #Dynamic_Programming #Depth_First_Search #Tree
44
// #2025_05_27_Time_27_ms_(100.00%)_Space_45.29_MB_(82.12%)
55

66
import java.util.ArrayList;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3501_3600.s3566_partition_array_into_two_equal_product_subsets;
2+
3+
// #Medium #Array #Bit_Manipulation #Recursion #Enumeration
4+
// #2025_06_03_Time_0_ms_(100.00%)_Space_42.45_MB_(36.66%)
5+
6+
public class Solution {
7+
public boolean checkEqualPartitions(int[] nums, long target) {
8+
for (int num : nums) {
9+
if (target % num != 0) {
10+
return false;
11+
}
12+
}
13+
long pro = 1;
14+
for (long n : nums) {
15+
pro *= n;
16+
}
17+
return pro == target * target;
18+
}
19+
}
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3501_3600.s3567_minimum_absolute_difference_in_sliding_submatrix;
2+
3+
// #Medium #Array #Sorting #Matrix #2025_06_03_Time_7_ms_(99.69%)_Space_45.24_MB_(99.03%)
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) {
28+
break;
29+
}
30+
}
31+
}
32+
result[x][y] = minDiff == Integer.MAX_VALUE ? 0 : minDiff;
33+
}
34+
}
35+
return result;
36+
}
37+
}
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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package g3501_3600.s3568_minimum_moves_to_clean_the_classroom;
2+
3+
// #Medium #Array #Hash_Table #Breadth_First_Search #Matrix #Bit_Manipulation
4+
// #2025_06_03_Time_94_ms_(99.86%)_Space_53.76_MB_(99.86%)
5+
6+
import java.util.ArrayDeque;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.Queue;
11+
12+
@SuppressWarnings({"java:S135", "java:S6541"})
13+
public class Solution {
14+
private static class State {
15+
int x;
16+
int y;
17+
int energy;
18+
int mask;
19+
int steps;
20+
21+
State(int x, int y, int energy, int mask, int steps) {
22+
this.x = x;
23+
this.y = y;
24+
this.energy = energy;
25+
this.mask = mask;
26+
this.steps = steps;
27+
}
28+
}
29+
30+
public int minMoves(String[] classroom, int energy) {
31+
int m = classroom.length;
32+
int n = classroom[0].length();
33+
char[][] grid = new char[m][n];
34+
for (int i = 0; i < m; i++) {
35+
grid[i] = classroom[i].toCharArray();
36+
}
37+
int startX = -1;
38+
int startY = -1;
39+
List<int[]> lumetarkon = new ArrayList<>();
40+
for (int i = 0; i < m; i++) {
41+
for (int j = 0; j < n; j++) {
42+
char c = grid[i][j];
43+
if (c == 'S') {
44+
startX = i;
45+
startY = j;
46+
} else if (c == 'L') {
47+
lumetarkon.add(new int[] {i, j});
48+
}
49+
}
50+
}
51+
int totalLitter = lumetarkon.size();
52+
int allMask = (1 << totalLitter) - 1;
53+
int[][][] visited = new int[m][n][1 << totalLitter];
54+
for (int[][] layer : visited) {
55+
for (int[] row : layer) {
56+
Arrays.fill(row, -1);
57+
}
58+
}
59+
Queue<State> queue = new ArrayDeque<>();
60+
queue.offer(new State(startX, startY, energy, 0, 0));
61+
visited[startX][startY][0] = energy;
62+
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
63+
while (!queue.isEmpty()) {
64+
State curr = queue.poll();
65+
if (curr.mask == allMask) {
66+
return curr.steps;
67+
}
68+
for (int[] dir : dirs) {
69+
int nx = curr.x + dir[0];
70+
int ny = curr.y + dir[1];
71+
if (nx < 0 || ny < 0 || nx >= m || ny >= n || grid[nx][ny] == 'X') {
72+
continue;
73+
}
74+
int nextEnergy = curr.energy - 1;
75+
if (nextEnergy < 0) {
76+
continue;
77+
}
78+
char cell = grid[nx][ny];
79+
if (cell == 'R') {
80+
nextEnergy = energy;
81+
}
82+
int nextMask = curr.mask;
83+
if (cell == 'L') {
84+
for (int i = 0; i < lumetarkon.size(); i++) {
85+
int[] pos = lumetarkon.get(i);
86+
if (pos[0] == nx && pos[1] == ny) {
87+
nextMask |= (1 << i);
88+
break;
89+
}
90+
}
91+
}
92+
if (visited[nx][ny][nextMask] < nextEnergy) {
93+
visited[nx][ny][nextMask] = nextEnergy;
94+
queue.offer(new State(nx, ny, nextEnergy, nextMask, curr.steps + 1));
95+
}
96+
}
97+
}
98+
return -1;
99+
}
100+
}
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)