From ef51f16327a7e31a0a62171614a3fc38002347b8 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 18 Nov 2024 07:43:29 +0200 Subject: [PATCH 1/6] Added tasks 3354-3357 --- .../Solution.java | 28 ++++++++++ .../readme.md | 51 +++++++++++++++++ .../Solution.java | 26 +++++++++ .../readme.md | 51 +++++++++++++++++ .../Solution.java | 44 +++++++++++++++ .../readme.md | 53 ++++++++++++++++++ .../Solution.java | 55 +++++++++++++++++++ .../readme.md | 53 ++++++++++++++++++ .../SolutionTest.java | 19 +++++++ .../SolutionTest.java | 22 ++++++++ .../SolutionTest.java | 25 +++++++++ .../SolutionTest.java | 23 ++++++++ 12 files changed, 450 insertions(+) create mode 100644 src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.java create mode 100644 src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/readme.md create mode 100644 src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java create mode 100644 src/main/java/g3301_3400/s3355_zero_array_transformation_i/readme.md create mode 100644 src/main/java/g3301_3400/s3356_zero_array_transformation_ii/Solution.java create mode 100644 src/main/java/g3301_3400/s3356_zero_array_transformation_ii/readme.md create mode 100644 src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java create mode 100644 src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/readme.md create mode 100644 src/test/java/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3356_zero_array_transformation_ii/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.java diff --git a/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.java b/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.java new file mode 100644 index 000000000..63436c324 --- /dev/null +++ b/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.java @@ -0,0 +1,28 @@ +package g3301_3400.s3354_make_array_elements_equal_to_zero; + +// #Easy #2024_11_18_Time_1_ms_(100.00%)_Space_42.2_MB_(100.00%) + +public class Solution { + public int countValidSelections(int[] nums) { + int[] rightSum = new int[nums.length]; + int[] leftSum = new int[nums.length]; + int result = 0; + leftSum[0] = 0; + rightSum[nums.length - 1] = 0; + for (int i = 1; i < nums.length; i++) { + leftSum[i] = leftSum[i - 1] + nums[i - 1]; + } + for (int j = nums.length - 2; j >= 0; j--) { + rightSum[j] = rightSum[j + 1] + nums[j + 1]; + } + for (int k = 0; k < nums.length; k++) { + if (nums[k] == 0 && Math.abs(rightSum[k] - leftSum[k]) == 1) { + result++; + } + if (nums[k] == 0 && Math.abs(rightSum[k] - leftSum[k]) == 0) { + result += 2; + } + } + return result; + } +} diff --git a/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/readme.md b/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/readme.md new file mode 100644 index 000000000..123fbff41 --- /dev/null +++ b/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/readme.md @@ -0,0 +1,51 @@ +3354\. Make Array Elements Equal to Zero + +Easy + +You are given an integer array `nums`. + +Start by selecting a starting position `curr` such that `nums[curr] == 0`, and choose a movement **direction** of either left or right. + +After that, you repeat the following process: + +* If `curr` is out of the range `[0, n - 1]`, this process ends. +* If `nums[curr] == 0`, move in the current direction by **incrementing** `curr` if you are moving right, or **decrementing** `curr` if you are moving left. +* Else if `nums[curr] > 0`: + * Decrement `nums[curr]` by 1. + * **Reverse** your movement direction (left becomes right and vice versa). + * Take a step in your new direction. + +A selection of the initial position `curr` and movement direction is considered **valid** if every element in `nums` becomes 0 by the end of the process. + +Return the number of possible **valid** selections. + +**Example 1:** + +**Input:** nums = [1,0,2,0,3] + +**Output:** 2 + +**Explanation:** + +The only possible valid selections are the following: + +* Choose `curr = 3`, and a movement direction to the left. + * [1,0,2,**0**,3] -> [1,0,**2**,0,3] -> [1,0,1,**0**,3] -> [1,0,1,0,**3**] -> [1,0,1,**0**,2] -> [1,0,**1**,0,2] -> [1,0,0,**0**,2] -> [1,0,0,0,**2**] -> [1,0,0,**0**,1] -> [1,0,**0**,0,1] -> [1,**0**,0,0,1] -> [**1**,0,0,0,1] -> [0,**0**,0,0,1] -> [0,0,**0**,0,1] -> [0,0,0,**0**,1] -> [0,0,0,0,**1**] -> [0,0,0,0,0]. +* Choose `curr = 3`, and a movement direction to the right. + * [1,0,2,**0**,3] -> [1,0,2,0,**3**] -> [1,0,2,**0**,2] -> [1,0,**2**,0,2] -> [1,0,1,**0**,2] -> [1,0,1,0,**2**] -> [1,0,1,**0**,1] -> [1,0,**1**,0,1] -> [1,0,0,**0**,1] -> [1,0,0,0,**1**] -> [1,0,0,**0**,0] -> [1,0,**0**,0,0] -> [1,**0**,0,0,0] -> [**1**,0,0,0,0] -> [0,0,0,0,0]. + +**Example 2:** + +**Input:** nums = [2,3,4,0,4,1,0] + +**Output:** 0 + +**Explanation:** + +There are no possible valid selections. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `0 <= nums[i] <= 100` +* There is at least one element `i` where `nums[i] == 0`. \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java b/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java new file mode 100644 index 000000000..9085111c5 --- /dev/null +++ b/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java @@ -0,0 +1,26 @@ +package g3301_3400.s3355_zero_array_transformation_i; + +// #Medium #2024_11_18_Time_3_ms_(100.00%)_Space_94.7_MB_(100.00%) + +public class Solution { + public boolean isZeroArray(int[] nums, int[][] queries) { + int n = nums.length; + int sum = 0; + for (int num : nums) sum += num; + if (sum == 0) return true; + int[] diff = new int[n + 1]; + for (int[] q : queries) { + int low = q[0]; + int high = q[1]; + diff[low] -= 1; + if (high + 1 < n) diff[high + 1] += 1; + } + for (int i = 0; i < n; i++) { + if (i > 0) diff[i] += diff[i - 1]; + nums[i] += diff[i]; + sum += diff[i]; + if (nums[i] > 0) return false; + } + return sum <= 0; + } +} diff --git a/src/main/java/g3301_3400/s3355_zero_array_transformation_i/readme.md b/src/main/java/g3301_3400/s3355_zero_array_transformation_i/readme.md new file mode 100644 index 000000000..1ec464ee9 --- /dev/null +++ b/src/main/java/g3301_3400/s3355_zero_array_transformation_i/readme.md @@ -0,0 +1,51 @@ +3355\. Zero Array Transformation I + +Medium + +You are given an integer array `nums` of length `n` and a 2D array `queries`, where queries[i] = [li, ri]. + +For each `queries[i]`: + +* Select a subset of indices within the range [li, ri] in `nums`. +* Decrement the values at the selected indices by 1. + +A **Zero Array** is an array where all elements are equal to 0. + +Return `true` if it is _possible_ to transform `nums` into a **Zero Array** after processing all the queries sequentially, otherwise return `false`. + +A **subset** of an array is a selection of elements (possibly none) of the array. + +**Example 1:** + +**Input:** nums = [1,0,1], queries = [[0,2]] + +**Output:** true + +**Explanation:** + +* **For i = 0:** + * Select the subset of indices as `[0, 2]` and decrement the values at these indices by 1. + * The array will become `[0, 0, 0]`, which is a Zero Array. + +**Example 2:** + +**Input:** nums = [4,3,2,1], queries = [[1,3],[0,2]] + +**Output:** false + +**Explanation:** + +* **For i = 0:** + * Select the subset of indices as `[1, 2, 3]` and decrement the values at these indices by 1. + * The array will become `[4, 2, 1, 0]`. +* **For i = 1:** + * Select the subset of indices as `[0, 1, 2]` and decrement the values at these indices by 1. + * The array will become `[3, 1, 0, 0]`, which is not a Zero Array. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 0 <= nums[i] <= 105 +* 1 <= queries.length <= 105 +* `queries[i].length == 2` +* 0 <= li <= ri < nums.length \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/Solution.java b/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/Solution.java new file mode 100644 index 000000000..308c4f055 --- /dev/null +++ b/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/Solution.java @@ -0,0 +1,44 @@ +package g3301_3400.s3356_zero_array_transformation_ii; + +// #Medium #2024_11_18_Time_22_ms_(100.00%)_Space_113.5_MB_(100.00%) + +public class Solution { + public int minZeroArray(int[] nums, int[][] queries) { + int total = 0; + int l = 0; + int h = queries.length - 1; + for (int i : nums) { + total += i; + } + if (total == 0) { + return 0; + } + while (l <= h) { + int m = l + (h - l) / 2; + if (isPossible(nums, queries, total, m)) { + h = m - 1; + } else { + l = m + 1; + } + } + return h + 2 > queries.length ? -1 : h + 2; + } + + private boolean isPossible(int[] nums, int[][] queries, int total, int k) { + int[] res = new int[nums.length + 1]; + int sum = 0; + for (int i = 0; i <= k; i++) { + int[] q = queries[i]; + res[q[0]] += q[2]; + res[q[1] + 1] -= q[2]; + } + for (int i = 0; i < nums.length; i++) { + sum += res[i]; + if (nums[i] - sum > 0) { + return false; + } + total -= nums[i]; + } + return total == 0; + } +} diff --git a/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/readme.md b/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/readme.md new file mode 100644 index 000000000..0561449eb --- /dev/null +++ b/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/readme.md @@ -0,0 +1,53 @@ +3356\. Zero Array Transformation II + +Medium + +You are given an integer array `nums` of length `n` and a 2D array `queries` where queries[i] = [li, ri, vali]. + +Each `queries[i]` represents the following action on `nums`: + +* Decrement the value at each index in the range [li, ri] in `nums` by **at most** vali. +* The amount by which each value is decremented can be chosen **independently** for each index. + +A **Zero Array** is an array with all its elements equal to 0. + +Return the **minimum** possible **non-negative** value of `k`, such that after processing the first `k` queries in **sequence**, `nums` becomes a **Zero Array**. If no such `k` exists, return -1. + +**Example 1:** + +**Input:** nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]] + +**Output:** 2 + +**Explanation:** + +* **For i = 0 (l = 0, r = 2, val = 1):** + * Decrement values at indices `[0, 1, 2]` by `[1, 0, 1]` respectively. + * The array will become `[1, 0, 1]`. +* **For i = 1 (l = 0, r = 2, val = 1):** + * Decrement values at indices `[0, 1, 2]` by `[1, 0, 1]` respectively. + * The array will become `[0, 0, 0]`, which is a Zero Array. Therefore, the minimum value of `k` is 2. + +**Example 2:** + +**Input:** nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]] + +**Output:** \-1 + +**Explanation:** + +* **For i = 0 (l = 1, r = 3, val = 2):** + * Decrement values at indices `[1, 2, 3]` by `[2, 2, 1]` respectively. + * The array will become `[4, 1, 0, 0]`. +* **For i = 1 (l = 0, r = 2, val \= 1):** + * Decrement values at indices `[0, 1, 2]` by `[1, 1, 0]` respectively. + * The array will become `[3, 0, 0, 0]`, which is not a Zero Array. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 0 <= nums[i] <= 5 * 105 +* 1 <= queries.length <= 105 +* `queries[i].length == 3` +* 0 <= li <= ri < nums.length +* 1 <= vali <= 5 \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java new file mode 100644 index 000000000..ae631b93d --- /dev/null +++ b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java @@ -0,0 +1,55 @@ +package g3301_3400.s3357_minimize_the_maximum_adjacent_element_difference; + +// #Hard #2024_11_18_Time_5_ms_(100.00%)_Space_58.9_MB_(100.00%) + +public class Solution { + public int minDifference(int[] nums) { + int n = nums.length; + int max_adj = 0; + int mina = Integer.MAX_VALUE; + int maxb = Integer.MIN_VALUE; + for (int i = 0; i < n - 1; ++i) { + int a = nums[i]; + int b = nums[i + 1]; + if (a > 0 && b > 0) { + max_adj = Math.max(max_adj, Math.abs(a - b)); + } else if (a > 0 || b > 0) { + mina = Math.min(mina, Math.max(a, b)); + maxb = Math.max(maxb, Math.max(a, b)); + } + } + + int res = 0; + for (int i = 0; i < n; ++i) { + if ((i > 0 && nums[i - 1] == -1) || nums[i] > 0) { + continue; + } + int j = i; + while (j < n && nums[j] == -1) { + j++; + } + int a = Integer.MAX_VALUE, b = Integer.MIN_VALUE; + if (i > 0) { + a = Math.min(a, nums[i - 1]); + b = Math.max(b, nums[i - 1]); + } + if (j < n) { + a = Math.min(a, nums[j]); + b = Math.max(b, nums[j]); + } + if (a <= b) { + if (j - i == 1) { + res = Math.max(res, Math.min(maxb - a, b - mina)); + } else { + res = + Math.max( + res, + Math.min( + maxb - a, + Math.min(b - mina, (maxb - mina + 2) / 3 * 2))); + } + } + } + return Math.max(max_adj, (res + 1) / 2); + } +} diff --git a/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/readme.md b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/readme.md new file mode 100644 index 000000000..b345c7bab --- /dev/null +++ b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/readme.md @@ -0,0 +1,53 @@ +3357\. Minimize the Maximum Adjacent Element Difference + +Hard + +You are given an array of integers `nums`. Some values in `nums` are **missing** and are denoted by -1. + +You can choose a pair of **positive** integers `(x, y)` **exactly once** and replace each **missing** element with _either_ `x` or `y`. + +You need to **minimize** the **maximum** **absolute difference** between _adjacent_ elements of `nums` after replacements. + +Return the **minimum** possible difference. + +**Example 1:** + +**Input:** nums = [1,2,-1,10,8] + +**Output:** 4 + +**Explanation:** + +By choosing the pair as `(6, 7)`, nums can be changed to `[1, 2, 6, 10, 8]`. + +The absolute differences between adjacent elements are: + +* `|1 - 2| == 1` +* `|2 - 6| == 4` +* `|6 - 10| == 4` +* `|10 - 8| == 2` + +**Example 2:** + +**Input:** nums = [-1,-1,-1] + +**Output:** 0 + +**Explanation:** + +By choosing the pair as `(4, 4)`, nums can be changed to `[4, 4, 4]`. + +**Example 3:** + +**Input:** nums = [-1,10,-1,8] + +**Output:** 1 + +**Explanation:** + +By choosing the pair as `(11, 9)`, nums can be changed to `[11, 10, 9, 8]`. + +**Constraints:** + +* 2 <= nums.length <= 105 +* `nums[i]` is either -1 or in the range [1, 109]. \ No newline at end of file diff --git a/src/test/java/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.java b/src/test/java/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.java new file mode 100644 index 000000000..ca0810dd7 --- /dev/null +++ b/src/test/java/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.java @@ -0,0 +1,19 @@ +package g3301_3400.s3354_make_array_elements_equal_to_zero; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countValidSelections() { + assertThat(new Solution().countValidSelections(new int[] {1, 0, 2, 0, 3}), equalTo(2)); + } + + @Test + void countValidSelections2() { + assertThat( + new Solution().countValidSelections(new int[] {2, 3, 4, 0, 4, 1, 0}), equalTo(0)); + } +} diff --git a/src/test/java/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.java b/src/test/java/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.java new file mode 100644 index 000000000..1b8e99d08 --- /dev/null +++ b/src/test/java/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.java @@ -0,0 +1,22 @@ +package g3301_3400.s3355_zero_array_transformation_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void isZeroArray() { + assertThat( + new Solution().isZeroArray(new int[] {1, 0, 1}, new int[][] {{0, 2}}), + equalTo(true)); + } + + @Test + void isZeroArray2() { + assertThat( + new Solution().isZeroArray(new int[] {4, 3, 2, 1}, new int[][] {{1, 3}, {0, 2}}), + equalTo(false)); + } +} diff --git a/src/test/java/g3301_3400/s3356_zero_array_transformation_ii/SolutionTest.java b/src/test/java/g3301_3400/s3356_zero_array_transformation_ii/SolutionTest.java new file mode 100644 index 000000000..fbde58ae8 --- /dev/null +++ b/src/test/java/g3301_3400/s3356_zero_array_transformation_ii/SolutionTest.java @@ -0,0 +1,25 @@ +package g3301_3400.s3356_zero_array_transformation_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minZeroArray() { + assertThat( + new Solution() + .minZeroArray( + new int[] {2, 0, 2}, new int[][] {{0, 2, 1}, {0, 2, 1}, {1, 1, 3}}), + equalTo(2)); + } + + @Test + void minZeroArray2() { + assertThat( + new Solution() + .minZeroArray(new int[] {4, 3, 2, 1}, new int[][] {{1, 3, 2}, {0, 2, 1}}), + equalTo(-1)); + } +} diff --git a/src/test/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.java b/src/test/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.java new file mode 100644 index 000000000..6916d3425 --- /dev/null +++ b/src/test/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3357_minimize_the_maximum_adjacent_element_difference; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minDifference() { + assertThat(new Solution().minDifference(new int[] {1, 2, -1, 10, 8}), equalTo(4)); + } + + @Test + void minDifference2() { + assertThat(new Solution().minDifference(new int[] {-1, -1, -1}), equalTo(0)); + } + + @Test + void minDifference3() { + assertThat(new Solution().minDifference(new int[] {-1, 10, -1, 8}), equalTo(1)); + } +} From d4505768c1f8c7a7439a617e3e4677321d16b482 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 18 Nov 2024 07:47:30 +0200 Subject: [PATCH 2/6] Fixed style --- .../Solution.java | 20 ++++++++++++++----- .../Solution.java | 9 +++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java b/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java index 9085111c5..154810acf 100644 --- a/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java +++ b/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java @@ -6,20 +6,30 @@ public class Solution { public boolean isZeroArray(int[] nums, int[][] queries) { int n = nums.length; int sum = 0; - for (int num : nums) sum += num; - if (sum == 0) return true; + for (int num : nums) { + sum += num; + } + if (sum == 0) { + return true; + } int[] diff = new int[n + 1]; for (int[] q : queries) { int low = q[0]; int high = q[1]; diff[low] -= 1; - if (high + 1 < n) diff[high + 1] += 1; + if (high + 1 < n) { + diff[high + 1] += 1; + } } for (int i = 0; i < n; i++) { - if (i > 0) diff[i] += diff[i - 1]; + if (i > 0) { + diff[i] += diff[i - 1]; + } nums[i] += diff[i]; sum += diff[i]; - if (nums[i] > 0) return false; + if (nums[i] > 0) { + return false; + } } return sum <= 0; } diff --git a/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java index ae631b93d..b67436c50 100644 --- a/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java +++ b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java @@ -5,14 +5,14 @@ public class Solution { public int minDifference(int[] nums) { int n = nums.length; - int max_adj = 0; + int maxAdj = 0; int mina = Integer.MAX_VALUE; int maxb = Integer.MIN_VALUE; for (int i = 0; i < n - 1; ++i) { int a = nums[i]; int b = nums[i + 1]; if (a > 0 && b > 0) { - max_adj = Math.max(max_adj, Math.abs(a - b)); + maxAdj = Math.max(maxAdj, Math.abs(a - b)); } else if (a > 0 || b > 0) { mina = Math.min(mina, Math.max(a, b)); maxb = Math.max(maxb, Math.max(a, b)); @@ -28,7 +28,8 @@ public int minDifference(int[] nums) { while (j < n && nums[j] == -1) { j++; } - int a = Integer.MAX_VALUE, b = Integer.MIN_VALUE; + int a = Integer.MAX_VALUE; + int b = Integer.MIN_VALUE; if (i > 0) { a = Math.min(a, nums[i - 1]); b = Math.max(b, nums[i - 1]); @@ -50,6 +51,6 @@ public int minDifference(int[] nums) { } } } - return Math.max(max_adj, (res + 1) / 2); + return Math.max(maxAdj, (res + 1) / 2); } } From ffcc438a6ab1c28172b09ada13658520eb4acd8f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 18 Nov 2024 07:47:53 +0200 Subject: [PATCH 3/6] Fixed format --- .../Solution.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java index b67436c50..b7ea0a253 100644 --- a/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java +++ b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java @@ -18,7 +18,6 @@ public int minDifference(int[] nums) { maxb = Math.max(maxb, Math.max(a, b)); } } - int res = 0; for (int i = 0; i < n; ++i) { if ((i > 0 && nums[i - 1] == -1) || nums[i] > 0) { From c0c6d0ac30af857f5003e1c001f107e69b2d1eed Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 19 Nov 2024 03:30:15 +0200 Subject: [PATCH 4/6] Updated tags and solution --- .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 53 +++++++------------ .../Solution.java | 2 +- 4 files changed, 22 insertions(+), 37 deletions(-) diff --git a/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.java b/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.java index 63436c324..fcba6649c 100644 --- a/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.java +++ b/src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.java @@ -1,6 +1,6 @@ package g3301_3400.s3354_make_array_elements_equal_to_zero; -// #Easy #2024_11_18_Time_1_ms_(100.00%)_Space_42.2_MB_(100.00%) +// #Easy #Array #Simulation #Prefix_Sum #2024_11_19_Time_1_ms_(95.09%)_Space_41.9_MB_(92.55%) public class Solution { public int countValidSelections(int[] nums) { diff --git a/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java b/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java index 154810acf..8ca0f6420 100644 --- a/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java +++ b/src/main/java/g3301_3400/s3355_zero_array_transformation_i/Solution.java @@ -1,6 +1,6 @@ package g3301_3400.s3355_zero_array_transformation_i; -// #Medium #2024_11_18_Time_3_ms_(100.00%)_Space_94.7_MB_(100.00%) +// #Medium #Array #Prefix_Sum #2024_11_19_Time_3_ms_(91.34%)_Space_96_MB_(17.22%) public class Solution { public boolean isZeroArray(int[] nums, int[][] queries) { diff --git a/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/Solution.java b/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/Solution.java index 308c4f055..9241894c6 100644 --- a/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/Solution.java +++ b/src/main/java/g3301_3400/s3356_zero_array_transformation_ii/Solution.java @@ -1,44 +1,29 @@ package g3301_3400.s3356_zero_array_transformation_ii; -// #Medium #2024_11_18_Time_22_ms_(100.00%)_Space_113.5_MB_(100.00%) +// #Medium #Array #Binary_Search #Prefix_Sum #2024_11_19_Time_4_ms_(93.46%)_Space_118.5_MB_(13.87%) public class Solution { public int minZeroArray(int[] nums, int[][] queries) { - int total = 0; - int l = 0; - int h = queries.length - 1; - for (int i : nums) { - total += i; - } - if (total == 0) { - return 0; - } - while (l <= h) { - int m = l + (h - l) / 2; - if (isPossible(nums, queries, total, m)) { - h = m - 1; - } else { - l = m + 1; - } - } - return h + 2 > queries.length ? -1 : h + 2; - } - - private boolean isPossible(int[] nums, int[][] queries, int total, int k) { - int[] res = new int[nums.length + 1]; - int sum = 0; - for (int i = 0; i <= k; i++) { - int[] q = queries[i]; - res[q[0]] += q[2]; - res[q[1] + 1] -= q[2]; - } + int[] diff = new int[nums.length]; + int idx = 0; + int d = 0; for (int i = 0; i < nums.length; i++) { - sum += res[i]; - if (nums[i] - sum > 0) { - return false; + d += diff[i]; + while (nums[i] + d > 0 && idx < queries.length) { + int[] q = queries[idx]; + if (i >= q[0] && i <= q[1]) { + d -= q[2]; + } + diff[q[0]] -= q[2]; + if (q[1] + 1 < nums.length) { + diff[q[1] + 1] += q[2]; + } + idx++; + } + if (nums[i] + d > 0) { + return -1; } - total -= nums[i]; } - return total == 0; + return idx; } } diff --git a/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java index b7ea0a253..9b2bf3042 100644 --- a/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java +++ b/src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.java @@ -1,6 +1,6 @@ package g3301_3400.s3357_minimize_the_maximum_adjacent_element_difference; -// #Hard #2024_11_18_Time_5_ms_(100.00%)_Space_58.9_MB_(100.00%) +// #Hard #Array #Greedy #Binary_Search #2024_11_19_Time_5_ms_(100.00%)_Space_59.2_MB_(29.41%) public class Solution { public int minDifference(int[] nums) { From 9642e02b943ae8c038e346426527568c4d2934a0 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 19 Nov 2024 07:27:55 +0200 Subject: [PATCH 5/6] Added tests --- build.gradle | 11 +++++++++-- .../SolutionTest.java | 8 ++++++++ .../SolutionTest.java | 7 +++++++ .../SolutionTest.java | 5 +++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 39f4fefc3..3cc8329d4 100644 --- a/build.gradle +++ b/build.gradle @@ -18,8 +18,15 @@ dependencies { testRuntimeOnly 'org.junit.platform:junit-platform-launcher:[1.11.3,)' } -test { - useJUnitPlatform() +testing { + suites { + test { + useJUnitJupiter() + } + } +} + +tasks.withType(Test).configureEach { maxParallelForks = Runtime.runtime.availableProcessors() } diff --git a/src/test/java/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.java b/src/test/java/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.java index ca0810dd7..8d6c14598 100644 --- a/src/test/java/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.java +++ b/src/test/java/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.java @@ -16,4 +16,12 @@ void countValidSelections2() { assertThat( new Solution().countValidSelections(new int[] {2, 3, 4, 0, 4, 1, 0}), equalTo(0)); } + + @Test + void countValidSelections3() { + assertThat( + new Solution() + .countValidSelections(new int[] {16, 13, 10, 0, 0, 0, 10, 6, 7, 8, 7}), + equalTo(3)); + } } diff --git a/src/test/java/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.java b/src/test/java/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.java index 1b8e99d08..d8741997d 100644 --- a/src/test/java/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.java +++ b/src/test/java/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.java @@ -19,4 +19,11 @@ void isZeroArray2() { new Solution().isZeroArray(new int[] {4, 3, 2, 1}, new int[][] {{1, 3}, {0, 2}}), equalTo(false)); } + + @Test + void isZeroArray3() { + assertThat( + new Solution().isZeroArray(new int[] {-1, 0, 1}, new int[][] {{1, 3}, {0, 2}}), + equalTo(true)); + } } diff --git a/src/test/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.java b/src/test/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.java index 6916d3425..11564cc5d 100644 --- a/src/test/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.java +++ b/src/test/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.java @@ -20,4 +20,9 @@ void minDifference2() { void minDifference3() { assertThat(new Solution().minDifference(new int[] {-1, 10, -1, 8}), equalTo(1)); } + + @Test + void minDifference4() { + assertThat(new Solution().minDifference(new int[] {14, -1, -1, 46}), equalTo(11)); + } } From 6e5a0f45b740fb80637f654dea403c43f37528c8 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 19 Nov 2024 07:31:51 +0200 Subject: [PATCH 6/6] Restored build.gradle --- build.gradle | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index 3cc8329d4..39f4fefc3 100644 --- a/build.gradle +++ b/build.gradle @@ -18,15 +18,8 @@ dependencies { testRuntimeOnly 'org.junit.platform:junit-platform-launcher:[1.11.3,)' } -testing { - suites { - test { - useJUnitJupiter() - } - } -} - -tasks.withType(Test).configureEach { +test { + useJUnitPlatform() maxParallelForks = Runtime.runtime.availableProcessors() }