Skip to content

Added tasks 3566-3569 #1988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 3, 2025
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
1 change: 0 additions & 1 deletion src/main/java/g0101_0200/s0178_rank_scores/script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ FROM
Scores
ORDER BY
Rank ASC;

1 change: 0 additions & 1 deletion src/main/java/g0101_0200/s0182_duplicate_emails/script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ GROUP BY
Email
HAVING
COUNT(Email) > 1;

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g3501_3600.s3558_number_of_ways_to_assign_edge_weights_i;

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

public class Solution {
private static int mod = (int) 1e9 + 7;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g3501_3600.s3559_number_of_ways_to_assign_edge_weights_ii;

// #Hard #Array #Dynamic_Programming #Math #Tree #Depth_First_Search
// #Hard #Array #Dynamic_Programming #Math #Depth_First_Search #Tree
// #2025_05_27_Time_138_ms_(64.66%)_Space_133.20_MB_(11.56%)

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g3501_3600.s3562_maximum_profit_from_trading_stocks_with_discounts;

// #Hard #Array #Dynamic_Programming #Tree #Depth_First_Search
// #Hard #Array #Dynamic_Programming #Depth_First_Search #Tree
// #2025_05_27_Time_27_ms_(100.00%)_Space_45.29_MB_(82.12%)

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3501_3600.s3566_partition_array_into_two_equal_product_subsets;

// #Medium #Array #Bit_Manipulation #Recursion #Enumeration
// #2025_06_03_Time_0_ms_(100.00%)_Space_42.45_MB_(36.66%)

public class Solution {
public boolean checkEqualPartitions(int[] nums, long target) {
for (int num : nums) {
if (target % num != 0) {
return false;
}
}
long pro = 1;
for (long n : nums) {
pro *= n;
}
return pro == target * target;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
3566\. Partition Array into Two Equal Product Subsets

Medium

You are given an integer array `nums` containing **distinct** positive integers and an integer `target`.

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

Return `true` if such a partition exists and `false` otherwise.

A **subset** of an array is a selection of elements of the array.

**Example 1:**

**Input:** nums = [3,1,6,8,4], target = 24

**Output:** true

**Explanation:** The subsets `[3, 8]` and `[1, 6, 4]` each have a product of 24. Hence, the output is true.

**Example 2:**

**Input:** nums = [2,5,3,7], target = 15

**Output:** false

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

**Constraints:**

* `3 <= nums.length <= 12`
* <code>1 <= target <= 10<sup>15</sup></code>
* `1 <= nums[i] <= 100`
* All elements of `nums` are **distinct**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package g3501_3600.s3567_minimum_absolute_difference_in_sliding_submatrix;

// #Medium #Array #Sorting #Matrix #2025_06_03_Time_7_ms_(99.69%)_Space_45.24_MB_(99.03%)

import java.util.Arrays;

public class Solution {
public int[][] minAbsDiff(int[][] grid, int k) {
int rows = grid.length;
int cols = grid[0].length;
int[][] result = new int[rows - k + 1][cols - k + 1];
for (int x = 0; x <= rows - k; x++) {
for (int y = 0; y <= cols - k; y++) {
int size = k * k;
int[] elements = new int[size];
int idx = 0;
for (int i = x; i < x + k; i++) {
for (int j = y; j < y + k; j++) {
elements[idx++] = grid[i][j];
}
}
Arrays.sort(elements);
int minDiff = Integer.MAX_VALUE;
for (int i = 1; i < size; i++) {
if (elements[i] != elements[i - 1]) {
minDiff = Math.min(minDiff, elements[i] - elements[i - 1]);
if (minDiff == 1) {
break;
}
}
}
result[x][y] = minDiff == Integer.MAX_VALUE ? 0 : minDiff;
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
3567\. Minimum Absolute Difference in Sliding Submatrix

Medium

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

For every contiguous `k x k` **submatrix** of `grid`, compute the **minimum absolute** difference between any two **distinct** values within that **submatrix**.

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

**Note**: If all elements in the submatrix have the same value, the answer will be 0.

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

**Example 1:**

**Input:** grid = [[1,8],[3,-2]], k = 2

**Output:** [[2]]

**Explanation:**

* There is only one possible `k x k` submatrix: `[[1, 8], [3, -2]]`.
* Distinct values in the submatrix are `[1, 8, 3, -2]`.
* The minimum absolute difference in the submatrix is `|1 - 3| = 2`. Thus, the answer is `[[2]]`.

**Example 2:**

**Input:** grid = [[3,-1]], k = 1

**Output:** [[0,0]]

**Explanation:**

* Both `k x k` submatrix has only one distinct element.
* Thus, the answer is `[[0, 0]]`.

**Example 3:**

**Input:** grid = [[1,-2,3],[2,3,5]], k = 2

**Output:** [[1,2]]

**Explanation:**

* There are two possible `k × k` submatrix:
* Starting at `(0, 0)`: `[[1, -2], [2, 3]]`.
* Distinct values in the submatrix are `[1, -2, 2, 3]`.
* The minimum absolute difference in the submatrix is `|1 - 2| = 1`.
* Starting at `(0, 1)`: `[[-2, 3], [3, 5]]`.
* Distinct values in the submatrix are `[-2, 3, 5]`.
* The minimum absolute difference in the submatrix is `|3 - 5| = 2`.
* Thus, the answer is `[[1, 2]]`.

**Constraints:**

* `1 <= m == grid.length <= 30`
* `1 <= n == grid[i].length <= 30`
* <code>-10<sup>5</sup> <= grid[i][j] <= 10<sup>5</sup></code>
* `1 <= k <= min(m, n)`
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package g3501_3600.s3568_minimum_moves_to_clean_the_classroom;

// #Medium #Array #Hash_Table #Breadth_First_Search #Matrix #Bit_Manipulation
// #2025_06_03_Time_94_ms_(99.86%)_Space_53.76_MB_(99.86%)

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;

@SuppressWarnings({"java:S135", "java:S6541"})
public class Solution {
private static class State {
int x;
int y;
int energy;
int mask;
int steps;

State(int x, int y, int energy, int mask, int steps) {
this.x = x;
this.y = y;
this.energy = energy;
this.mask = mask;
this.steps = steps;
}
}

public int minMoves(String[] classroom, int energy) {
int m = classroom.length;
int n = classroom[0].length();
char[][] grid = new char[m][n];
for (int i = 0; i < m; i++) {
grid[i] = classroom[i].toCharArray();
}
int startX = -1;
int startY = -1;
List<int[]> lumetarkon = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
char c = grid[i][j];
if (c == 'S') {
startX = i;
startY = j;
} else if (c == 'L') {
lumetarkon.add(new int[] {i, j});
}
}
}
int totalLitter = lumetarkon.size();
int allMask = (1 << totalLitter) - 1;
int[][][] visited = new int[m][n][1 << totalLitter];
for (int[][] layer : visited) {
for (int[] row : layer) {
Arrays.fill(row, -1);
}
}
Queue<State> queue = new ArrayDeque<>();
queue.offer(new State(startX, startY, energy, 0, 0));
visited[startX][startY][0] = energy;
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
while (!queue.isEmpty()) {
State curr = queue.poll();
if (curr.mask == allMask) {
return curr.steps;
}
for (int[] dir : dirs) {
int nx = curr.x + dir[0];
int ny = curr.y + dir[1];
if (nx < 0 || ny < 0 || nx >= m || ny >= n || grid[nx][ny] == 'X') {
continue;
}
int nextEnergy = curr.energy - 1;
if (nextEnergy < 0) {
continue;
}
char cell = grid[nx][ny];
if (cell == 'R') {
nextEnergy = energy;
}
int nextMask = curr.mask;
if (cell == 'L') {
for (int i = 0; i < lumetarkon.size(); i++) {
int[] pos = lumetarkon.get(i);
if (pos[0] == nx && pos[1] == ny) {
nextMask |= (1 << i);
break;
}
}
}
if (visited[nx][ny][nextMask] < nextEnergy) {
visited[nx][ny][nextMask] = nextEnergy;
queue.offer(new State(nx, ny, nextEnergy, nextMask, curr.steps + 1));
}
}
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
3568\. Minimum Moves to Clean the Classroom

Medium

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:

* `'S'`: Starting position of the student
* `'L'`: Litter that must be collected (once collected, the cell becomes empty)
* `'R'`: Reset area that restores the student's energy to full capacity, regardless of their current energy level (can be used multiple times)
* `'X'`: Obstacle the student cannot pass through
* `'.'`: Empty space

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

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

Return the **minimum** number of moves required to collect all litter items, or `-1` if it's impossible.

**Example 1:**

**Input:** classroom = ["S.", "XL"], energy = 2

**Output:** 2

**Explanation:**

* The student starts at cell `(0, 0)` with 2 units of energy.
* Since cell `(1, 0)` contains an obstacle 'X', the student cannot move directly downward.
* A valid sequence of moves to collect all litter is as follows:
* Move 1: From `(0, 0)` → `(0, 1)` with 1 unit of energy and 1 unit remaining.
* Move 2: From `(0, 1)` → `(1, 1)` to collect the litter `'L'`.
* The student collects all the litter using 2 moves. Thus, the output is 2.

**Example 2:**

**Input:** classroom = ["LS", "RL"], energy = 4

**Output:** 3

**Explanation:**

* The student starts at cell `(0, 1)` with 4 units of energy.
* A valid sequence of moves to collect all litter is as follows:
* Move 1: From `(0, 1)` → `(0, 0)` to collect the first litter `'L'` with 1 unit of energy used and 3 units remaining.
* Move 2: From `(0, 0)` → `(1, 0)` to `'R'` to reset and restore energy back to 4.
* Move 3: From `(1, 0)` → `(1, 1)` to collect the second litter `'L'`.
* The student collects all the litter using 3 moves. Thus, the output is 3.

**Example 3:**

**Input:** classroom = ["L.S", "RXL"], energy = 3

**Output:** \-1

**Explanation:**

No valid path collects all `'L'`.

**Constraints:**

* `1 <= m == classroom.length <= 20`
* `1 <= n == classroom[i].length <= 20`
* `classroom[i][j]` is one of `'S'`, `'L'`, `'R'`, `'X'`, or `'.'`
* `1 <= energy <= 50`
* There is exactly **one** `'S'` in the grid.
* There are **at most** 10 `'L'` cells in the grid.
Loading