From cc70059d1cb9bc6f1e96022cb89d4fb5cec34c81 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 19 Nov 2024 10:12:08 +0200 Subject: [PATCH] Added tasks 3354-3357 --- .../Solution.kt | 30 ++++++++++ .../readme.md | 51 ++++++++++++++++ .../Solution.kt | 36 +++++++++++ .../readme.md | 51 ++++++++++++++++ .../Solution.kt | 29 +++++++++ .../readme.md | 53 ++++++++++++++++ .../Solution.kt | 60 +++++++++++++++++++ .../readme.md | 53 ++++++++++++++++ .../SolutionTest.kt | 31 ++++++++++ .../SolutionTest.kt | 38 ++++++++++++ .../SolutionTest.kt | 28 +++++++++ .../SolutionTest.kt | 36 +++++++++++ 12 files changed, 496 insertions(+) create mode 100644 src/main/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3355_zero_array_transformation_i/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3355_zero_array_transformation_i/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3356_zero_array_transformation_ii/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3356_zero_array_transformation_ii/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/readme.md create mode 100644 src/test/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.kt create mode 100644 src/test/kotlin/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3301_3400/s3356_zero_array_transformation_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.kt diff --git a/src/main/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.kt b/src/main/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.kt new file mode 100644 index 000000000..9d9f6d9cb --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/Solution.kt @@ -0,0 +1,30 @@ +package g3301_3400.s3354_make_array_elements_equal_to_zero + +// #Easy #Array #Simulation #Prefix_Sum #2024_11_19_Time_153_ms_(96.67%)_Space_35.4_MB_(93.33%) + +import kotlin.math.abs + +class Solution { + fun countValidSelections(nums: IntArray): Int { + val rightSum = IntArray(nums.size) + val leftSum = IntArray(nums.size) + var result = 0 + leftSum[0] = 0 + rightSum[nums.size - 1] = 0 + for (i in 1.rangeUntil(nums.size)) { + leftSum[i] = leftSum[i - 1] + nums[i - 1] + } + for (j in nums.size - 2 downTo 0) { + rightSum[j] = rightSum[j + 1] + nums[j + 1] + } + for (k in nums.indices) { + if (nums[k] == 0 && abs((rightSum[k] - leftSum[k])) == 1) { + result++ + } + if (nums[k] == 0 && abs((rightSum[k] - leftSum[k])) == 0) { + result += 2 + } + } + return result + } +} diff --git a/src/main/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/readme.md b/src/main/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/readme.md new file mode 100644 index 000000000..123fbff41 --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3301_3400/s3355_zero_array_transformation_i/Solution.kt b/src/main/kotlin/g3301_3400/s3355_zero_array_transformation_i/Solution.kt new file mode 100644 index 000000000..d67104082 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3355_zero_array_transformation_i/Solution.kt @@ -0,0 +1,36 @@ +package g3301_3400.s3355_zero_array_transformation_i + +// #Medium #Array #Prefix_Sum #2024_11_19_Time_6_ms_(36.84%)_Space_94_MB_(100.00%) + +class Solution { + fun isZeroArray(nums: IntArray, queries: Array): Boolean { + val n = nums.size + var sum = 0 + for (num in nums) { + sum += num + } + if (sum == 0) { + return true + } + val diff = IntArray(n + 1) + for (q in queries) { + val low = q[0] + val high = q[1] + diff[low] -= 1 + if (high + 1 < n) { + diff[high + 1] += 1 + } + } + for (i in 0.rangeUntil(n)) { + 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/kotlin/g3301_3400/s3355_zero_array_transformation_i/readme.md b/src/main/kotlin/g3301_3400/s3355_zero_array_transformation_i/readme.md new file mode 100644 index 000000000..1ec464ee9 --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3301_3400/s3356_zero_array_transformation_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3356_zero_array_transformation_ii/Solution.kt new file mode 100644 index 000000000..4c95ea0d4 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3356_zero_array_transformation_ii/Solution.kt @@ -0,0 +1,29 @@ +package g3301_3400.s3356_zero_array_transformation_ii + +// #Medium #Array #Binary_Search #Prefix_Sum #2024_11_19_Time_5_ms_(100.00%)_Space_132.4_MB_(46.67%) + +class Solution { + fun minZeroArray(nums: IntArray, queries: Array): Int { + val diff = IntArray(nums.size) + var idx = 0 + var d = 0 + for (i in nums.indices) { + d += diff[i] + while (nums[i] + d > 0 && idx < queries.size) { + val q = queries[idx] + if (i >= q[0] && i <= q[1]) { + d -= q[2] + } + diff[q[0]] -= q[2] + if (q[1] + 1 < nums.size) { + diff[q[1] + 1] += q[2] + } + idx++ + } + if (nums[i] + d > 0) { + return -1 + } + } + return idx + } +} diff --git a/src/main/kotlin/g3301_3400/s3356_zero_array_transformation_ii/readme.md b/src/main/kotlin/g3301_3400/s3356_zero_array_transformation_ii/readme.md new file mode 100644 index 000000000..0561449eb --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.kt b/src/main/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.kt new file mode 100644 index 000000000..40bf06406 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/Solution.kt @@ -0,0 +1,60 @@ +package g3301_3400.s3357_minimize_the_maximum_adjacent_element_difference + +// #Hard #Array #Greedy #Binary_Search #2024_11_19_Time_13_ms_(100.00%)_Space_53.6_MB_(100.00%) + +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun minDifference(nums: IntArray): Int { + val n = nums.size + var maxAdj = 0 + var mina = Int.Companion.MAX_VALUE + var maxb = Int.Companion.MIN_VALUE + for (i in 0.rangeUntil(n - 1)) { + val a = nums[i] + val b = nums[i + 1] + if (a > 0 && b > 0) { + maxAdj = max(maxAdj, abs((a - b))) + } else if (a > 0 || b > 0) { + mina = min(mina, max(a, b)) + maxb = max(maxb, max(a, b)) + } + } + var res = 0 + for (i in 0.rangeUntil(n)) { + if ((i > 0 && nums[i - 1] == -1) || nums[i] > 0) { + continue + } + var j = i + while (j < n && nums[j] == -1) { + j++ + } + var a = Int.Companion.MAX_VALUE + var b = Int.Companion.MIN_VALUE + if (i > 0) { + a = min(a, nums[i - 1]) + b = max(b, nums[i - 1]) + } + if (j < n) { + a = min(a, nums[j]) + b = max(b, nums[j]) + } + if (a <= b) { + if (j - i == 1) { + res = max(res, min((maxb - a), (b - mina))) + } else { + res = max( + res, + min( + maxb - a, + min(b - mina, (maxb - mina + 2) / 3 * 2) + ) + ) + } + } + } + return max(maxAdj, (res + 1) / 2) + } +} diff --git a/src/main/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/readme.md b/src/main/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/readme.md new file mode 100644 index 000000000..b345c7bab --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.kt new file mode 100644 index 000000000..902e26996 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3301_3400.s3354_make_array_elements_equal_to_zero + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countValidSelections() { + assertThat( + Solution().countValidSelections(intArrayOf(1, 0, 2, 0, 3)), + equalTo(2) + ) + } + + @Test + fun countValidSelections2() { + assertThat( + Solution().countValidSelections(intArrayOf(2, 3, 4, 0, 4, 1, 0)), equalTo(0) + ) + } + + @Test + fun countValidSelections3() { + assertThat( + Solution() + .countValidSelections(intArrayOf(16, 13, 10, 0, 0, 0, 10, 6, 7, 8, 7)), + equalTo(3) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.kt new file mode 100644 index 000000000..efff23361 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3355_zero_array_transformation_i/SolutionTest.kt @@ -0,0 +1,38 @@ +package g3301_3400.s3355_zero_array_transformation_i + +import org.hamcrest.CoreMatchers +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun isZeroArray() { + assertThat( + Solution() + .isZeroArray(intArrayOf(1, 0, 1), arrayOf(intArrayOf(0, 2))), + CoreMatchers.equalTo(true) + ) + } + + @Test + fun isZeroArray2() { + assertThat( + Solution().isZeroArray( + intArrayOf(4, 3, 2, 1), + arrayOf(intArrayOf(1, 3), intArrayOf(0, 2)) + ), + CoreMatchers.equalTo(false) + ) + } + + @Test + fun isZeroArray3() { + assertThat( + Solution().isZeroArray( + intArrayOf(-1, 0, 1), + arrayOf(intArrayOf(1, 3), intArrayOf(0, 2)) + ), + CoreMatchers.equalTo(true) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3356_zero_array_transformation_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3356_zero_array_transformation_ii/SolutionTest.kt new file mode 100644 index 000000000..6a7a6e7fb --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3356_zero_array_transformation_ii/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3301_3400.s3356_zero_array_transformation_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minZeroArray() { + assertThat( + Solution() + .minZeroArray( + intArrayOf(2, 0, 2), + arrayOf(intArrayOf(0, 2, 1), intArrayOf(0, 2, 1), intArrayOf(1, 1, 3)) + ), + equalTo(2) + ) + } + + @Test + fun minZeroArray2() { + assertThat( + Solution() + .minZeroArray(intArrayOf(4, 3, 2, 1), arrayOf(intArrayOf(1, 3, 2), intArrayOf(0, 2, 1))), + equalTo(-1) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.kt new file mode 100644 index 000000000..232c8e7dd --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference/SolutionTest.kt @@ -0,0 +1,36 @@ +package g3301_3400.s3357_minimize_the_maximum_adjacent_element_difference + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minDifference() { + assertThat( + Solution().minDifference(intArrayOf(1, 2, -1, 10, 8)), + equalTo(4) + ) + } + + @Test + fun minDifference2() { + assertThat(Solution().minDifference(intArrayOf(-1, -1, -1)), equalTo(0)) + } + + @Test + fun minDifference3() { + assertThat( + Solution().minDifference(intArrayOf(-1, 10, -1, 8)), + equalTo(1) + ) + } + + @Test + fun minDifference4() { + assertThat( + Solution().minDifference(intArrayOf(14, -1, -1, 46)), + equalTo(11) + ) + } +}