Skip to content

Commit 6dbfafb

Browse files
committed
Added task 3566-3569
1 parent 3039214 commit 6dbfafb

File tree

12 files changed

+633
-0
lines changed

12 files changed

+633
-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_1_ms_(100.00%)_Space_42.26_MB_(100.00%)
4+
5+
class Solution {
6+
fun checkEqualPartitions(nums: IntArray, target: Long): Boolean {
7+
for (num in nums) {
8+
if (target % num != 0L) {
9+
return false
10+
}
11+
}
12+
var pro: Long = 1
13+
for (n in 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: 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 #2025_06_01_Time_18_ms_(100.00%)_Space_53.66_MB_(25.00%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun minAbsDiff(grid: Array<IntArray>, k: Int): Array<IntArray> {
9+
val rows = grid.size
10+
val cols = grid[0].size
11+
val result = Array<IntArray>(rows - k + 1) { IntArray(cols - k + 1) }
12+
for (x in 0..rows - k) {
13+
for (y in 0..cols - k) {
14+
val size = k * k
15+
val elements = IntArray(size)
16+
var idx = 0
17+
for (i in x..<x + k) {
18+
for (j in y..<y + k) {
19+
elements[idx++] = grid[i][j]
20+
}
21+
}
22+
elements.sort()
23+
var minDiff = Int.Companion.MAX_VALUE
24+
for (i in 1..<size) {
25+
if (elements[i] != elements[i - 1]) {
26+
minDiff = min(minDiff, elements[i] - elements[i - 1])
27+
if (minDiff == 1) {
28+
break
29+
}
30+
}
31+
}
32+
result[x][y] = if (minDiff == Int.Companion.MAX_VALUE) 0 else 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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package g3501_3600.s3568_minimum_moves_to_clean_the_classroom
2+
3+
// #Medium #2025_06_01_Time_149_ms_(100.00%)_Space_64.20_MB_(100.00%)
4+
5+
import java.util.ArrayDeque
6+
import java.util.Queue
7+
8+
class Solution {
9+
private class State(var x: Int, var y: Int, var energy: Int, var mask: Int, var steps: Int)
10+
11+
fun minMoves(classroom: Array<String>, energy: Int): Int {
12+
val m = classroom.size
13+
val n = classroom[0].length
14+
val grid = Array<CharArray>(m) { CharArray(n) }
15+
for (i in 0..<m) {
16+
grid[i] = classroom[i].toCharArray()
17+
}
18+
var startX = -1
19+
var startY = -1
20+
val lumetarkon: MutableList<IntArray> = ArrayList<IntArray>()
21+
for (i in 0..<m) {
22+
for (j in 0..<n) {
23+
val c = grid[i][j]
24+
if (c == 'S') {
25+
startX = i
26+
startY = j
27+
} else if (c == 'L') {
28+
lumetarkon.add(intArrayOf(i, j))
29+
}
30+
}
31+
}
32+
val totalLitter = lumetarkon.size
33+
val allMask = (1 shl totalLitter) - 1
34+
val visited: Array<Array<IntArray>> =
35+
Array<Array<IntArray>>(m) { Array<IntArray>(n) { IntArray(1 shl totalLitter) } }
36+
for (layer in visited) {
37+
for (row in layer) {
38+
row.fill(-1)
39+
}
40+
}
41+
val queue: Queue<State> = ArrayDeque<State>()
42+
queue.offer(State(startX, startY, energy, 0, 0))
43+
visited[startX][startY][0] = energy
44+
val dirs = arrayOf<IntArray>(intArrayOf(0, 1), intArrayOf(1, 0), intArrayOf(0, -1), intArrayOf(-1, 0))
45+
while (!queue.isEmpty()) {
46+
val curr = queue.poll()
47+
if (curr.mask == allMask) {
48+
return curr.steps
49+
}
50+
for (dir in dirs) {
51+
val nx = curr.x + dir[0]
52+
val ny = curr.y + dir[1]
53+
if (nx < 0 || ny < 0 || nx >= m || ny >= n || grid[nx][ny] == 'X') {
54+
continue
55+
}
56+
var nextEnergy = curr.energy - 1
57+
if (nextEnergy < 0) {
58+
continue
59+
}
60+
val cell = grid[nx][ny]
61+
if (cell == 'R') {
62+
nextEnergy = energy
63+
}
64+
var nextMask = curr.mask
65+
if (cell == 'L') {
66+
for (i in lumetarkon.indices) {
67+
val pos = lumetarkon[i]
68+
if (pos[0] == nx && pos[1] == ny) {
69+
nextMask = nextMask or (1 shl i)
70+
break
71+
}
72+
}
73+
}
74+
if (visited[nx][ny][nextMask] < nextEnergy) {
75+
visited[nx][ny][nextMask] = nextEnergy
76+
queue.offer(State(nx, ny, nextEnergy, nextMask, curr.steps + 1))
77+
}
78+
}
79+
}
80+
return -1
81+
}
82+
}
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)