Skip to content

Commit 3e6bc51

Browse files
committed
Added tasks 3566-3569
1 parent b5bfce0 commit 3e6bc51

File tree

5 files changed

+540
-3
lines changed
  • src/main/java/g3501_3600
    • s3566_partition_array_into_two_equal_product_subsets
    • s3567_minimum_absolute_difference_in_sliding_submatrix
    • s3568_minimum_moves_to_clean_the_classroom
    • s3569_maximize_count_of_distinct_primes_after_split

5 files changed

+540
-3
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,13 +2088,17 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3569 |[Maximize Count of Distinct Primes After Split](src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split)| Hard | Array, Math, Segment_Tree, Number_Theory | 280 | 97.30
2092+
| 3568 |[Minimum Moves to Clean the Classroom](src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom)| Medium | Array, Hash_Table, Breadth_First_Search, Matrix, Bit_Manipulation | 94 | 99.86
2093+
| 3567 |[Minimum Absolute Difference in Sliding Submatrix](src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix)| Medium | Array, Sorting, Matrix | 7 | 99.69
2094+
| 3566 |[Partition Array into Two Equal Product Subsets](src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets)| Medium | Array, Bit_Manipulation, Recursion, Enumeration | 0 | 100.00
20912095
| 3564 |[Seasonal Sales Analysis](src/main/java/g3501_3600/s3564_seasonal_sales_analysis)| Medium | Database | 505 | 100.00
20922096
| 3563 |[Lexicographically Smallest String After Adjacent Removals](src/main/java/g3501_3600/s3563_lexicographically_smallest_string_after_adjacent_removals)| Hard | String, Dynamic_Programming | 121 | 99.09
2093-
| 3562 |[Maximum Profit from Trading Stocks with Discounts](src/main/java/g3501_3600/s3562_maximum_profit_from_trading_stocks_with_discounts)| Hard | Array, Dynamic_Programming, Tree, Depth_First_Search | 27 | 100.00
2097+
| 3562 |[Maximum Profit from Trading Stocks with Discounts](src/main/java/g3501_3600/s3562_maximum_profit_from_trading_stocks_with_discounts)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree | 27 | 100.00
20942098
| 3561 |[Resulting String After Adjacent Removals](src/main/java/g3501_3600/s3561_resulting_string_after_adjacent_removals)| Medium | String, Stack, Simulation | 36 | 100.00
20952099
| 3560 |[Find Minimum Log Transportation Cost](src/main/java/g3501_3600/s3560_find_minimum_log_transportation_cost)| Easy | Math | 0 | 100.00
2096-
| 3559 |[Number of Ways to Assign Edge Weights II](src/main/java/g3501_3600/s3559_number_of_ways_to_assign_edge_weights_ii)| Hard | Array, Dynamic_Programming, Math, Tree, Depth_First_Search | 138 | 64.66
2097-
| 3558 |[Number of Ways to Assign Edge Weights I](src/main/java/g3501_3600/s3558_number_of_ways_to_assign_edge_weights_i)| Medium | Math, Tree, Depth_First_Search | 12 | 100.00
2100+
| 3559 |[Number of Ways to Assign Edge Weights II](src/main/java/g3501_3600/s3559_number_of_ways_to_assign_edge_weights_ii)| Hard | Array, Dynamic_Programming, Math, Depth_First_Search, Tree | 138 | 64.66
2101+
| 3558 |[Number of Ways to Assign Edge Weights I](src/main/java/g3501_3600/s3558_number_of_ways_to_assign_edge_weights_i)| Medium | Math, Depth_First_Search, Tree | 12 | 100.00
20982102
| 3557 |[Find Maximum Number of Non Intersecting Substrings](src/main/java/g3501_3600/s3557_find_maximum_number_of_non_intersecting_substrings)| Medium | String, Hash_Table, Dynamic_Programming, Greedy | 15 | 84.54
20992103
| 3556 |[Sum of Largest Prime Substrings](src/main/java/g3501_3600/s3556_sum_of_largest_prime_substrings)| Medium | String, Hash_Table, Math, Sorting, Number_Theory | 7 | 99.93
21002104
| 3554 |[Find Category Recommendation Pairs](src/main/java/g3501_3600/s3554_find_category_recommendation_pairs)| Hard | Database | 623 | 82.76
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3566\. Partition Array into Two Equal Product Subsets
5+
6+
Medium
7+
8+
You are given an integer array `nums` containing **distinct** positive integers and an integer `target`.
9+
10+
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`.
11+
12+
Return `true` if such a partition exists and `false` otherwise.
13+
14+
A **subset** of an array is a selection of elements of the array.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [3,1,6,8,4], target = 24
19+
20+
**Output:** true
21+
22+
**Explanation:** The subsets `[3, 8]` and `[1, 6, 4]` each have a product of 24. Hence, the output is true.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [2,5,3,7], target = 15
27+
28+
**Output:** false
29+
30+
**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.
31+
32+
**Constraints:**
33+
34+
* `3 <= nums.length <= 12`
35+
* <code>1 <= target <= 10<sup>15</sup></code>
36+
* `1 <= nums[i] <= 100`
37+
* All elements of `nums` are **distinct**.
38+
39+
## Solution
40+
41+
```java
42+
public class Solution {
43+
public boolean checkEqualPartitions(int[] nums, long target) {
44+
for (int num : nums) {
45+
if (target % num != 0) {
46+
return false;
47+
}
48+
}
49+
long pro = 1;
50+
for (long n : nums) {
51+
pro *= n;
52+
}
53+
return pro == target * target;
54+
}
55+
}
56+
```
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3567\. Minimum Absolute Difference in Sliding Submatrix
5+
6+
Medium
7+
8+
You are given an `m x n` integer matrix `grid` and an integer `k`.
9+
10+
For every contiguous `k x k` **submatrix** of `grid`, compute the **minimum absolute** difference between any two **distinct** values within that **submatrix**.
11+
12+
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`.
13+
14+
**Note**: If all elements in the submatrix have the same value, the answer will be 0.
15+
16+
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`.
17+
18+
**Example 1:**
19+
20+
**Input:** grid = \[\[1,8],[3,-2]], k = 2
21+
22+
**Output:** [[2]]
23+
24+
**Explanation:**
25+
26+
* There is only one possible `k x k` submatrix: `[[1, 8], [3, -2]]`.
27+
* Distinct values in the submatrix are `[1, 8, 3, -2]`.
28+
* The minimum absolute difference in the submatrix is `|1 - 3| = 2`. Thus, the answer is `[[2]]`.
29+
30+
**Example 2:**
31+
32+
**Input:** grid = \[\[3,-1]], k = 1
33+
34+
**Output:** [[0,0]]
35+
36+
**Explanation:**
37+
38+
* Both `k x k` submatrix has only one distinct element.
39+
* Thus, the answer is `[[0, 0]]`.
40+
41+
**Example 3:**
42+
43+
**Input:** grid = \[\[1,-2,3],[2,3,5]], k = 2
44+
45+
**Output:** [[1,2]]
46+
47+
**Explanation:**
48+
49+
* There are two possible `k × k` submatrix:
50+
* Starting at `(0, 0)`: `[[1, -2], [2, 3]]`.
51+
* Distinct values in the submatrix are `[1, -2, 2, 3]`.
52+
* The minimum absolute difference in the submatrix is `|1 - 2| = 1`.
53+
* Starting at `(0, 1)`: `[[-2, 3], [3, 5]]`.
54+
* Distinct values in the submatrix are `[-2, 3, 5]`.
55+
* The minimum absolute difference in the submatrix is `|3 - 5| = 2`.
56+
* Thus, the answer is `[[1, 2]]`.
57+
58+
**Constraints:**
59+
60+
* `1 <= m == grid.length <= 30`
61+
* `1 <= n == grid[i].length <= 30`
62+
* <code>-10<sup>5</sup> <= grid[i][j] <= 10<sup>5</sup></code>
63+
* `1 <= k <= min(m, n)`
64+
65+
## Solution
66+
67+
```java
68+
import java.util.Arrays;
69+
70+
public class Solution {
71+
public int[][] minAbsDiff(int[][] grid, int k) {
72+
int rows = grid.length;
73+
int cols = grid[0].length;
74+
int[][] result = new int[rows - k + 1][cols - k + 1];
75+
for (int x = 0; x <= rows - k; x++) {
76+
for (int y = 0; y <= cols - k; y++) {
77+
int size = k * k;
78+
int[] elements = new int[size];
79+
int idx = 0;
80+
for (int i = x; i < x + k; i++) {
81+
for (int j = y; j < y + k; j++) {
82+
elements[idx++] = grid[i][j];
83+
}
84+
}
85+
Arrays.sort(elements);
86+
int minDiff = Integer.MAX_VALUE;
87+
for (int i = 1; i < size; i++) {
88+
if (elements[i] != elements[i - 1]) {
89+
minDiff = Math.min(minDiff, elements[i] - elements[i - 1]);
90+
if (minDiff == 1) {
91+
break;
92+
}
93+
}
94+
}
95+
result[x][y] = minDiff == Integer.MAX_VALUE ? 0 : minDiff;
96+
}
97+
}
98+
return result;
99+
}
100+
}
101+
```
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3568\. Minimum Moves to Clean the Classroom
5+
6+
Medium
7+
8+
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:
9+
10+
* `'S'`: Starting position of the student
11+
* `'L'`: Litter that must be collected (once collected, the cell becomes empty)
12+
* `'R'`: Reset area that restores the student's energy to full capacity, regardless of their current energy level (can be used multiple times)
13+
* `'X'`: Obstacle the student cannot pass through
14+
* `'.'`: Empty space
15+
16+
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'`.
17+
18+
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`.
19+
20+
Return the **minimum** number of moves required to collect all litter items, or `-1` if it's impossible.
21+
22+
**Example 1:**
23+
24+
**Input:** classroom = ["S.", "XL"], energy = 2
25+
26+
**Output:** 2
27+
28+
**Explanation:**
29+
30+
* The student starts at cell `(0, 0)` with 2 units of energy.
31+
* Since cell `(1, 0)` contains an obstacle 'X', the student cannot move directly downward.
32+
* A valid sequence of moves to collect all litter is as follows:
33+
* Move 1: From `(0, 0)``(0, 1)` with 1 unit of energy and 1 unit remaining.
34+
* Move 2: From `(0, 1)``(1, 1)` to collect the litter `'L'`.
35+
* The student collects all the litter using 2 moves. Thus, the output is 2.
36+
37+
**Example 2:**
38+
39+
**Input:** classroom = ["LS", "RL"], energy = 4
40+
41+
**Output:** 3
42+
43+
**Explanation:**
44+
45+
* The student starts at cell `(0, 1)` with 4 units of energy.
46+
* A valid sequence of moves to collect all litter is as follows:
47+
* Move 1: From `(0, 1)``(0, 0)` to collect the first litter `'L'` with 1 unit of energy used and 3 units remaining.
48+
* Move 2: From `(0, 0)``(1, 0)` to `'R'` to reset and restore energy back to 4.
49+
* Move 3: From `(1, 0)``(1, 1)` to collect the second litter `'L'`.
50+
* The student collects all the litter using 3 moves. Thus, the output is 3.
51+
52+
**Example 3:**
53+
54+
**Input:** classroom = ["L.S", "RXL"], energy = 3
55+
56+
**Output:** \-1
57+
58+
**Explanation:**
59+
60+
No valid path collects all `'L'`.
61+
62+
**Constraints:**
63+
64+
* `1 <= m == classroom.length <= 20`
65+
* `1 <= n == classroom[i].length <= 20`
66+
* `classroom[i][j]` is one of `'S'`, `'L'`, `'R'`, `'X'`, or `'.'`
67+
* `1 <= energy <= 50`
68+
* There is exactly **one** `'S'` in the grid.
69+
* There are **at most** 10 `'L'` cells in the grid.
70+
71+
## Solution
72+
73+
```java
74+
import java.util.ArrayDeque;
75+
import java.util.ArrayList;
76+
import java.util.Arrays;
77+
import java.util.List;
78+
import java.util.Queue;
79+
80+
@SuppressWarnings({"java:S135", "java:S6541"})
81+
public class Solution {
82+
private static class State {
83+
int x;
84+
int y;
85+
int energy;
86+
int mask;
87+
int steps;
88+
89+
State(int x, int y, int energy, int mask, int steps) {
90+
this.x = x;
91+
this.y = y;
92+
this.energy = energy;
93+
this.mask = mask;
94+
this.steps = steps;
95+
}
96+
}
97+
98+
public int minMoves(String[] classroom, int energy) {
99+
int m = classroom.length;
100+
int n = classroom[0].length();
101+
char[][] grid = new char[m][n];
102+
for (int i = 0; i < m; i++) {
103+
grid[i] = classroom[i].toCharArray();
104+
}
105+
int startX = -1;
106+
int startY = -1;
107+
List<int[]> lumetarkon = new ArrayList<>();
108+
for (int i = 0; i < m; i++) {
109+
for (int j = 0; j < n; j++) {
110+
char c = grid[i][j];
111+
if (c == 'S') {
112+
startX = i;
113+
startY = j;
114+
} else if (c == 'L') {
115+
lumetarkon.add(new int[] {i, j});
116+
}
117+
}
118+
}
119+
int totalLitter = lumetarkon.size();
120+
int allMask = (1 << totalLitter) - 1;
121+
int[][][] visited = new int[m][n][1 << totalLitter];
122+
for (int[][] layer : visited) {
123+
for (int[] row : layer) {
124+
Arrays.fill(row, -1);
125+
}
126+
}
127+
Queue<State> queue = new ArrayDeque<>();
128+
queue.offer(new State(startX, startY, energy, 0, 0));
129+
visited[startX][startY][0] = energy;
130+
int[][] dirs = { {0, 1}, {1, 0}, {0, -1}, {-1, 0}};
131+
while (!queue.isEmpty()) {
132+
State curr = queue.poll();
133+
if (curr.mask == allMask) {
134+
return curr.steps;
135+
}
136+
for (int[] dir : dirs) {
137+
int nx = curr.x + dir[0];
138+
int ny = curr.y + dir[1];
139+
if (nx < 0 || ny < 0 || nx >= m || ny >= n || grid[nx][ny] == 'X') {
140+
continue;
141+
}
142+
int nextEnergy = curr.energy - 1;
143+
if (nextEnergy < 0) {
144+
continue;
145+
}
146+
char cell = grid[nx][ny];
147+
if (cell == 'R') {
148+
nextEnergy = energy;
149+
}
150+
int nextMask = curr.mask;
151+
if (cell == 'L') {
152+
for (int i = 0; i < lumetarkon.size(); i++) {
153+
int[] pos = lumetarkon.get(i);
154+
if (pos[0] == nx && pos[1] == ny) {
155+
nextMask |= (1 << i);
156+
break;
157+
}
158+
}
159+
}
160+
if (visited[nx][ny][nextMask] < nextEnergy) {
161+
visited[nx][ny][nextMask] = nextEnergy;
162+
queue.offer(new State(nx, ny, nextEnergy, nextMask, curr.steps + 1));
163+
}
164+
}
165+
}
166+
return -1;
167+
}
168+
}
169+
```

0 commit comments

Comments
 (0)