From 01aa41c33dfe0033e279b6202f01443b6014280a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 1 Jun 2025 16:24:33 +0300 Subject: [PATCH 1/9] Added tasks 3566-3569 --- .../Solution.java | 18 ++ .../readme.md | 34 ++++ .../Solution.java | 35 ++++ .../readme.md | 60 +++++++ .../Solution.java | 96 +++++++++++ .../readme.md | 66 ++++++++ .../Solution.java | 155 ++++++++++++++++++ .../readme.md | 47 ++++++ .../SolutionTest.java | 20 +++ .../SolutionTest.java | 28 ++++ .../SolutionTest.java | 23 +++ .../SolutionTest.java | 23 +++ 12 files changed, 605 insertions(+) create mode 100644 src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java create mode 100644 src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/readme.md create mode 100644 src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java create mode 100644 src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/readme.md create mode 100644 src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java create mode 100644 src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/readme.md create mode 100644 src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java create mode 100644 src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/readme.md create mode 100644 src/test/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/SolutionTest.java create mode 100644 src/test/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/SolutionTest.java create mode 100644 src/test/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/SolutionTest.java create mode 100644 src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java diff --git a/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java b/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java new file mode 100644 index 000000000..0ef7da2f8 --- /dev/null +++ b/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java @@ -0,0 +1,18 @@ +package g3501_3600.s3566_partition_array_into_two_equal_product_subsets; + +// #Medium #2025_06_01_Time_0_ms_(100.00%)_Space_42.01_MB_(78.42%) + +public class Solution { + public boolean checkEqualPartitions(int[] nums, long target) { + for (int num : nums) { + if (target % (long) num != 0) { + return false; + } + } + long pro = 1; + for (long n : nums) { + pro *= n; + } + return pro == target * target; + } +} diff --git a/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/readme.md b/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/readme.md new file mode 100644 index 000000000..74bdaf0d7 --- /dev/null +++ b/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/readme.md @@ -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` +* 1 <= target <= 1015 +* `1 <= nums[i] <= 100` +* All elements of `nums` are **distinct**. \ No newline at end of file diff --git a/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java b/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java new file mode 100644 index 000000000..df7ce68a9 --- /dev/null +++ b/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java @@ -0,0 +1,35 @@ +package g3501_3600.s3567_minimum_absolute_difference_in_sliding_submatrix; + +// #Medium #2025_06_01_Time_7_ms_(99.65%)_Space_46.08_MB_(10.21%) + +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; + } +} diff --git a/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/readme.md b/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/readme.md new file mode 100644 index 000000000..31d2e1f60 --- /dev/null +++ b/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/readme.md @@ -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` +* -105 <= grid[i][j] <= 105 +* `1 <= k <= min(m, n)` \ No newline at end of file diff --git a/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java new file mode 100644 index 000000000..af046a827 --- /dev/null +++ b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java @@ -0,0 +1,96 @@ +package g3501_3600.s3568_minimum_moves_to_clean_the_classroom; + +// #Medium #2025_06_01_Time_90_ms_(100.00%)_Space_53.82_MB_(99.66%) + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Queue; + +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, 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, startY = -1; + List 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 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; + } +} diff --git a/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/readme.md b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/readme.md new file mode 100644 index 000000000..421faa12c --- /dev/null +++ b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/readme.md @@ -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. \ No newline at end of file diff --git a/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java new file mode 100644 index 000000000..601ef4c2c --- /dev/null +++ b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java @@ -0,0 +1,155 @@ +package g3501_3600.s3569_maximize_count_of_distinct_primes_after_split; + +// #Hard #2025_06_01_Time_294_ms_(96.15%)_Space_76.16_MB_(61.54%) + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.TreeSet; + +public class Solution { + private static final int MAX_VAL = 100005; + private static boolean[] isPrime = new boolean[MAX_VAL]; + + static { + Arrays.fill(isPrime, true); + isPrime[0] = isPrime[1] = false; + for (int i = 2; i * i < MAX_VAL; i++) { + if (isPrime[i]) { + for (int j = i * i; j < MAX_VAL; j += i) { + isPrime[j] = false; + } + } + } + } + + private static class Node { + int maxVal; + int lazyDelta; + } + + private static class SegmentTree { + Node[] tree; + int N; + + public SegmentTree(int n) { + this.N = n; + tree = new Node[4 * N]; + for (int i = 0; i < 4 * N; i++) { + tree[i] = new Node(); + } + } + + private void push(int nodeIdx) { + if (tree[nodeIdx].lazyDelta != 0) { + tree[2 * nodeIdx].maxVal += tree[nodeIdx].lazyDelta; + tree[2 * nodeIdx].lazyDelta += tree[nodeIdx].lazyDelta; + tree[2 * nodeIdx + 1].maxVal += tree[nodeIdx].lazyDelta; + tree[2 * nodeIdx + 1].lazyDelta += tree[nodeIdx].lazyDelta; + tree[nodeIdx].lazyDelta = 0; + } + } + + private void update(int queryStart, int queryEnd, int delta) { + queryStart = Math.max(1, queryStart); + queryEnd = Math.min(N - 1, queryEnd); + if (queryStart > queryEnd) { + return; + } + update(1, 1, N - 1, queryStart, queryEnd, delta); + } + + private void update( + int nodeIdx, int start, int end, int queryStart, int queryEnd, int delta) { + if (start > end || start > queryEnd || end < queryStart) { + return; + } + if (queryStart <= start && end <= queryEnd) { + tree[nodeIdx].maxVal += delta; + tree[nodeIdx].lazyDelta += delta; + return; + } + push(nodeIdx); + + int mid = (start + end) / 2; + update(2 * nodeIdx, start, mid, queryStart, queryEnd, delta); + update(2 * nodeIdx + 1, mid + 1, end, queryStart, queryEnd, delta); + tree[nodeIdx].maxVal = Math.max(tree[2 * nodeIdx].maxVal, tree[2 * nodeIdx + 1].maxVal); + } + + public int queryMax() { + if (N - 1 < 1) { + return 0; + } + return tree[1].maxVal; + } + } + + public int[] maximumCount(int[] nums, int[][] queries) { + int n = nums.length; + Map> primeIndices = new HashMap<>(); + for (int i = 0; i < n; i++) { + if (isPrime[nums[i]]) { + primeIndices.computeIfAbsent(nums[i], k -> new TreeSet<>()).add(i); + } + } + SegmentTree segmentTree = new SegmentTree(n); + for (Map.Entry> entry : primeIndices.entrySet()) { + TreeSet indices = entry.getValue(); + int first = indices.first(); + int last = indices.last(); + segmentTree.update(first + 1, last, 1); + } + int[] result = new int[queries.length]; + for (int q = 0; q < queries.length; q++) { + int idx = queries[q][0]; + int val = queries[q][1]; + int oldVal = nums[idx]; + if (isPrime[oldVal]) { + TreeSet indices = primeIndices.get(oldVal); + int oldFirst = indices.first(); + int oldLast = indices.last(); + indices.remove(idx); + if (indices.isEmpty()) { + primeIndices.remove(oldVal); + segmentTree.update(oldFirst + 1, oldLast, -1); + } else { + int newFirst = indices.first(); + int newLast = indices.last(); + + if (idx == oldFirst && newFirst != oldFirst) { + segmentTree.update(oldFirst + 1, newFirst, -1); + } + if (idx == oldLast && newLast != oldLast) { + segmentTree.update(newLast + 1, oldLast, -1); + } + } + } + nums[idx] = val; + if (isPrime[val]) { + boolean wasNewPrime = !primeIndices.containsKey(val); + TreeSet indices = primeIndices.computeIfAbsent(val, k -> new TreeSet<>()); + int oldFirst = indices.isEmpty() ? -1 : indices.first(); + int oldLast = indices.isEmpty() ? -1 : indices.last(); + indices.add(idx); + int newFirst = indices.first(); + int newLast = indices.last(); + if (wasNewPrime) { + segmentTree.update(newFirst + 1, newLast, 1); + } else { + if (idx < oldFirst) { + segmentTree.update(newFirst + 1, oldFirst, 1); + } + if (idx > oldLast) { + segmentTree.update(oldLast + 1, newLast, 1); + } + } + } + int totalDistinctPrimesInCurrentNums = primeIndices.size(); + int maxIntersection = segmentTree.queryMax(); + maxIntersection = Math.max(0, maxIntersection); + result[q] = totalDistinctPrimesInCurrentNums + maxIntersection; + } + return result; + } +} diff --git a/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/readme.md b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/readme.md new file mode 100644 index 000000000..042623001 --- /dev/null +++ b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/readme.md @@ -0,0 +1,47 @@ +3569\. Maximize Count of Distinct Primes After Split + +Hard + +You are given an integer array `nums` having length `n` and a 2D integer array `queries` where `queries[i] = [idx, val]`. + +For each query: + +1. Update `nums[idx] = val`. +2. Choose an integer `k` with `1 <= k < n` to split the array into the non-empty prefix `nums[0..k-1]` and suffix `nums[k..n-1]` such that the sum of the counts of **distinct** prime values in each part is **maximum**. + +**Note:** The changes made to the array in one query persist into the next query. + +Return an array containing the result for each query, in the order they are given. + +**Example 1:** + +**Input:** nums = [2,1,3,1,2], queries = [[1,2],[3,3]] + +**Output:** [3,4] + +**Explanation:** + +* Initially `nums = [2, 1, 3, 1, 2]`. +* After 1st query, `nums = [2, 2, 3, 1, 2]`. Split `nums` into `[2]` and `[2, 3, 1, 2]`. `[2]` consists of 1 distinct prime and `[2, 3, 1, 2]` consists of 2 distinct primes. Hence, the answer for this query is `1 + 2 = 3`. +* After 2nd query, `nums = [2, 2, 3, 3, 2]`. Split `nums` into `[2, 2, 3]` and `[3, 2]` with an answer of `2 + 2 = 4`. +* The output is `[3, 4]`. + +**Example 2:** + +**Input:** nums = [2,1,4], queries = [[0,1]] + +**Output:** [0] + +**Explanation:** + +* Initially `nums = [2, 1, 4]`. +* After 1st query, `nums = [1, 1, 4]`. There are no prime numbers in `nums`, hence the answer for this query is 0. +* The output is `[0]`. + +**Constraints:** + +* 2 <= n == nums.length <= 5 * 104 +* 1 <= queries.length <= 5 * 104 +* 1 <= nums[i] <= 105 +* `0 <= queries[i][0] < nums.length` +* 1 <= queries[i][1] <= 105 \ No newline at end of file diff --git a/src/test/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/SolutionTest.java b/src/test/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/SolutionTest.java new file mode 100644 index 000000000..957a2f424 --- /dev/null +++ b/src/test/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/SolutionTest.java @@ -0,0 +1,20 @@ +package g3501_3600.s3566_partition_array_into_two_equal_product_subsets; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void checkEqualPartitions() { + assertThat( + new Solution().checkEqualPartitions(new int[] {3, 1, 6, 8, 4}, 24L), equalTo(true)); + } + + @Test + void checkEqualPartitions2() { + assertThat( + new Solution().checkEqualPartitions(new int[] {2, 5, 3, 7}, 15L), equalTo(false)); + } +} diff --git a/src/test/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/SolutionTest.java b/src/test/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/SolutionTest.java new file mode 100644 index 000000000..08fdfbcce --- /dev/null +++ b/src/test/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/SolutionTest.java @@ -0,0 +1,28 @@ +package g3501_3600.s3567_minimum_absolute_difference_in_sliding_submatrix; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minAbsDiff() { + assertThat( + new Solution().minAbsDiff(new int[][] {{1, 8}, {3, -2}}, 2), + equalTo(new int[][] {{2}})); + } + + @Test + void minAbsDiff2() { + assertThat( + new Solution().minAbsDiff(new int[][] {{3, -1}}, 1), equalTo(new int[][] {{0, 0}})); + } + + @Test + void minAbsDiff3() { + assertThat( + new Solution().minAbsDiff(new int[][] {{1, -2, 3}, {2, 3, 5}}, 2), + equalTo(new int[][] {{1, 2}})); + } +} diff --git a/src/test/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/SolutionTest.java b/src/test/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/SolutionTest.java new file mode 100644 index 000000000..42c1d9085 --- /dev/null +++ b/src/test/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/SolutionTest.java @@ -0,0 +1,23 @@ +package g3501_3600.s3568_minimum_moves_to_clean_the_classroom; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minMoves() { + assertThat(new Solution().minMoves(new String[] {"S.", "XL"}, 2), equalTo(2)); + } + + @Test + void minMoves2() { + assertThat(new Solution().minMoves(new String[] {"LS", "RL"}, 4), equalTo(3)); + } + + @Test + void minMoves3() { + assertThat(new Solution().minMoves(new String[] {"L.S", "RXL"}, 3), equalTo(-1)); + } +} diff --git a/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java new file mode 100644 index 000000000..7ad060965 --- /dev/null +++ b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java @@ -0,0 +1,23 @@ +package g3501_3600.s3569_maximize_count_of_distinct_primes_after_split; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumCount() { + assertThat( + new Solution() + .maximumCount(new int[] {2, 1, 3, 1, 2}, new int[][] {{1, 2}, {3, 3}}), + equalTo(new int[] {3, 4})); + } + + @Test + void maximumCount2() { + assertThat( + new Solution().maximumCount(new int[] {2, 1, 4}, new int[][] {{0, 1}}), + equalTo(new int[] {0})); + } +} From e5d8e1178edfd1ad80d5a5d5e723feb78faf5a8e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 1 Jun 2025 16:27:57 +0300 Subject: [PATCH 2/9] Fixed style --- .../Solution.java | 4 +++- .../Solution.java | 6 ++++-- .../Solution.java | 14 +++++++------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java b/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java index df7ce68a9..1875f489f 100644 --- a/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java +++ b/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java @@ -24,7 +24,9 @@ public int[][] minAbsDiff(int[][] grid, int k) { 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; + if (minDiff == 1) { + break; + } } } result[x][y] = minDiff == Integer.MAX_VALUE ? 0 : minDiff; diff --git a/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java index af046a827..d002157aa 100644 --- a/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java +++ b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java @@ -26,12 +26,14 @@ private static class State { } public int minMoves(String[] classroom, int energy) { - int m = classroom.length, n = classroom[0].length(); + 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, startY = -1; + int startX = -1; + int startY = -1; List lumetarkon = new ArrayList<>(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { diff --git a/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java index 601ef4c2c..fe353782f 100644 --- a/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java +++ b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java @@ -30,12 +30,12 @@ private static class Node { private static class SegmentTree { Node[] tree; - int N; + int n; public SegmentTree(int n) { - this.N = n; - tree = new Node[4 * N]; - for (int i = 0; i < 4 * N; i++) { + this.n = n; + tree = new Node[4 * this.n]; + for (int i = 0; i < 4 * this.n; i++) { tree[i] = new Node(); } } @@ -52,11 +52,11 @@ private void push(int nodeIdx) { private void update(int queryStart, int queryEnd, int delta) { queryStart = Math.max(1, queryStart); - queryEnd = Math.min(N - 1, queryEnd); + queryEnd = Math.min(n - 1, queryEnd); if (queryStart > queryEnd) { return; } - update(1, 1, N - 1, queryStart, queryEnd, delta); + update(1, 1, n - 1, queryStart, queryEnd, delta); } private void update( @@ -78,7 +78,7 @@ private void update( } public int queryMax() { - if (N - 1 < 1) { + if (n - 1 < 1) { return 0; } return tree[1].maxVal; From 71598f7b23981ac69e4c6cbbb50e942b2efe1bfd Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 1 Jun 2025 16:31:55 +0300 Subject: [PATCH 3/9] Added test --- .../SolutionTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java index 7ad060965..f469bcc85 100644 --- a/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java +++ b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java @@ -20,4 +20,11 @@ void maximumCount2() { new Solution().maximumCount(new int[] {2, 1, 4}, new int[][] {{0, 1}}), equalTo(new int[] {0})); } + + @Test + void maximumCount3() { + assertThat( + new Solution().maximumCount(new int[] {2, 34}, new int[][] {{1, 2}, {1, 3}}), + equalTo(new int[] {2, 3})); + } } From ca70fcc6cc55ab2f7c307370383b4f2adc4866c5 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 1 Jun 2025 16:33:28 +0300 Subject: [PATCH 4/9] Added test --- .../SolutionTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java index f469bcc85..f004cc99e 100644 --- a/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java +++ b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java @@ -27,4 +27,11 @@ void maximumCount3() { new Solution().maximumCount(new int[] {2, 34}, new int[][] {{1, 2}, {1, 3}}), equalTo(new int[] {2, 3})); } + + @Test + void maximumCount4() { + assertThat( + new Solution().maximumCount(new int[] {4, 2}, new int[][] {{0, 2}, {0, 2}}), + equalTo(new int[] {2, 2})); + } } From 8898dfd49f0e272e85babd2e67f7e3ac4395e0cd Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 1 Jun 2025 16:34:51 +0300 Subject: [PATCH 5/9] Fixed sonar --- .../Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java b/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java index 0ef7da2f8..61faaede0 100644 --- a/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java +++ b/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java @@ -5,7 +5,7 @@ public class Solution { public boolean checkEqualPartitions(int[] nums, long target) { for (int num : nums) { - if (target % (long) num != 0) { + if (target % num != 0) { return false; } } From 4a0471ba4fa96ec9c6485c38d84c1f0598061c68 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 1 Jun 2025 16:36:50 +0300 Subject: [PATCH 6/9] Fixed sonar --- .../s3568_minimum_moves_to_clean_the_classroom/Solution.java | 1 + .../Solution.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java index d002157aa..f8854df1a 100644 --- a/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java +++ b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.Queue; +@SuppressWarnings({"java:S135", "java:S6541"}) public class Solution { private static class State { int x; diff --git a/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java index fe353782f..95fb72dc3 100644 --- a/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java +++ b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.TreeSet; +@SuppressWarnings("java:S6541") public class Solution { private static final int MAX_VAL = 100005; private static boolean[] isPrime = new boolean[MAX_VAL]; From 5cd418820592d3e6473fe65c46c4d212f9e87681 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 1 Jun 2025 18:44:24 +0300 Subject: [PATCH 7/9] Update SolutionTest.java --- .../SolutionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java index f004cc99e..e0167588c 100644 --- a/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java +++ b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java @@ -24,7 +24,7 @@ void maximumCount2() { @Test void maximumCount3() { assertThat( - new Solution().maximumCount(new int[] {2, 34}, new int[][] {{1, 2}, {1, 3}}), + new Solution().maximumCount(new int[] {2, 3}, new int[][] {{1, 2}, {1, 3}}), equalTo(new int[] {2, 3})); } From fbb2fbf6d691de5205c71997e34087908bf61db0 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 1 Jun 2025 18:47:31 +0300 Subject: [PATCH 8/9] Update SolutionTest.java --- .../SolutionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java index e0167588c..3f4a51693 100644 --- a/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java +++ b/src/test/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/SolutionTest.java @@ -24,8 +24,8 @@ void maximumCount2() { @Test void maximumCount3() { assertThat( - new Solution().maximumCount(new int[] {2, 3}, new int[][] {{1, 2}, {1, 3}}), - equalTo(new int[] {2, 3})); + new Solution().maximumCount(new int[] {2, 34}, new int[][] {{1, 2}, {1, 3}}), + equalTo(new int[] {2, 2})); } @Test From 1eb2028187ee1088b377b17dc80cda498dea4b62 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 3 Jun 2025 05:06:47 +0300 Subject: [PATCH 9/9] Updated tags --- src/main/java/g0101_0200/s0178_rank_scores/script.sql | 1 - src/main/java/g0101_0200/s0182_duplicate_emails/script.sql | 1 - .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 3 ++- .../Solution.java | 2 +- .../s3568_minimum_moves_to_clean_the_classroom/Solution.java | 3 ++- .../Solution.java | 3 ++- 9 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/g0101_0200/s0178_rank_scores/script.sql b/src/main/java/g0101_0200/s0178_rank_scores/script.sql index 0e78b870b..2773cd557 100644 --- a/src/main/java/g0101_0200/s0178_rank_scores/script.sql +++ b/src/main/java/g0101_0200/s0178_rank_scores/script.sql @@ -7,4 +7,3 @@ FROM Scores ORDER BY Rank ASC; - diff --git a/src/main/java/g0101_0200/s0182_duplicate_emails/script.sql b/src/main/java/g0101_0200/s0182_duplicate_emails/script.sql index 4dda6cf45..0cbebed39 100644 --- a/src/main/java/g0101_0200/s0182_duplicate_emails/script.sql +++ b/src/main/java/g0101_0200/s0182_duplicate_emails/script.sql @@ -8,4 +8,3 @@ GROUP BY Email HAVING COUNT(Email) > 1; - diff --git a/src/main/java/g3501_3600/s3558_number_of_ways_to_assign_edge_weights_i/Solution.java b/src/main/java/g3501_3600/s3558_number_of_ways_to_assign_edge_weights_i/Solution.java index 288e502dd..f06a99310 100644 --- a/src/main/java/g3501_3600/s3558_number_of_ways_to_assign_edge_weights_i/Solution.java +++ b/src/main/java/g3501_3600/s3558_number_of_ways_to_assign_edge_weights_i/Solution.java @@ -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; diff --git a/src/main/java/g3501_3600/s3559_number_of_ways_to_assign_edge_weights_ii/Solution.java b/src/main/java/g3501_3600/s3559_number_of_ways_to_assign_edge_weights_ii/Solution.java index 77d38483c..ce739a286 100644 --- a/src/main/java/g3501_3600/s3559_number_of_ways_to_assign_edge_weights_ii/Solution.java +++ b/src/main/java/g3501_3600/s3559_number_of_ways_to_assign_edge_weights_ii/Solution.java @@ -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; diff --git a/src/main/java/g3501_3600/s3562_maximum_profit_from_trading_stocks_with_discounts/Solution.java b/src/main/java/g3501_3600/s3562_maximum_profit_from_trading_stocks_with_discounts/Solution.java index c3b416e60..5d0233e4d 100644 --- a/src/main/java/g3501_3600/s3562_maximum_profit_from_trading_stocks_with_discounts/Solution.java +++ b/src/main/java/g3501_3600/s3562_maximum_profit_from_trading_stocks_with_discounts/Solution.java @@ -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; diff --git a/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java b/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java index 61faaede0..4fbfbed1b 100644 --- a/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java +++ b/src/main/java/g3501_3600/s3566_partition_array_into_two_equal_product_subsets/Solution.java @@ -1,6 +1,7 @@ package g3501_3600.s3566_partition_array_into_two_equal_product_subsets; -// #Medium #2025_06_01_Time_0_ms_(100.00%)_Space_42.01_MB_(78.42%) +// #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) { diff --git a/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java b/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java index 1875f489f..d36439d36 100644 --- a/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java +++ b/src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix/Solution.java @@ -1,6 +1,6 @@ package g3501_3600.s3567_minimum_absolute_difference_in_sliding_submatrix; -// #Medium #2025_06_01_Time_7_ms_(99.65%)_Space_46.08_MB_(10.21%) +// #Medium #Array #Sorting #Matrix #2025_06_03_Time_7_ms_(99.69%)_Space_45.24_MB_(99.03%) import java.util.Arrays; diff --git a/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java index f8854df1a..2e72c83cb 100644 --- a/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java +++ b/src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom/Solution.java @@ -1,6 +1,7 @@ package g3501_3600.s3568_minimum_moves_to_clean_the_classroom; -// #Medium #2025_06_01_Time_90_ms_(100.00%)_Space_53.82_MB_(99.66%) +// #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; diff --git a/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java index 95fb72dc3..71d10c6e2 100644 --- a/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java +++ b/src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split/Solution.java @@ -1,6 +1,7 @@ package g3501_3600.s3569_maximize_count_of_distinct_primes_after_split; -// #Hard #2025_06_01_Time_294_ms_(96.15%)_Space_76.16_MB_(61.54%) +// #Hard #Array #Math #Segment_Tree #Number_Theory +// #2025_06_03_Time_280_ms_(97.30%)_Space_76.62_MB_(52.25%) import java.util.Arrays; import java.util.HashMap;