Skip to content

Added tasks 3354-3357 #723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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.
* <code>[1,0,2,**<ins>0</ins>**,3] -> [1,0,**<ins>2</ins>**,0,3] -> [1,0,1,**<ins>0</ins>**,3] -> [1,0,1,0,**<ins>3</ins>**] -> [1,0,1,**<ins>0</ins>**,2] -> [1,0,**<ins>1</ins>**,0,2] -> [1,0,0,**<ins>0</ins>**,2] -> [1,0,0,0,**<ins>2</ins>**] -> [1,0,0,**<ins>0</ins>**,1] -> [1,0,**<ins>0</ins>**,0,1] -> [1,**<ins>0</ins>**,0,0,1] -> [**<ins>1</ins>**,0,0,0,1] -> [0,**<ins>0</ins>**,0,0,1] -> [0,0,**<ins>0</ins>**,0,1] -> [0,0,0,**<ins>0</ins>**,1] -> [0,0,0,0,**<ins>1</ins>**] -> [0,0,0,0,0]</code>.
* Choose `curr = 3`, and a movement direction to the right.
* <code>[1,0,2,**<ins>0</ins>**,3] -> [1,0,2,0,**<ins>3</ins>**] -> [1,0,2,**<ins>0</ins>**,2] -> [1,0,**<ins>2</ins>**,0,2] -> [1,0,1,**<ins>0</ins>**,2] -> [1,0,1,0,**<ins>2</ins>**] -> [1,0,1,**<ins>0</ins>**,1] -> [1,0,**<ins>1</ins>**,0,1] -> [1,0,0,**<ins>0</ins>**,1] -> [1,0,0,0,**<ins>1</ins>**] -> [1,0,0,**<ins>0</ins>**,0] -> [1,0,**<ins>0</ins>**,0,0] -> [1,**<ins>0</ins>**,0,0,0] -> [**<ins>1</ins>**,0,0,0,0] -> [0,0,0,0,0].</code>

**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`.
Original file line number Diff line number Diff line change
@@ -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<IntArray>): 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
}
}
Original file line number Diff line number Diff line change
@@ -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 <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.

For each `queries[i]`:

* Select a subset of indices within the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> 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:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
* <code>1 <= queries.length <= 10<sup>5</sup></code>
* `queries[i].length == 2`
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code>
Original file line number Diff line number Diff line change
@@ -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<IntArray>): 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
}
}
Original file line number Diff line number Diff line change
@@ -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 <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>, val<sub>i</sub>]</code>.

Each `queries[i]` represents the following action on `nums`:

* Decrement the value at each index in the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in `nums` by **at most** <code>val<sub>i</sub></code>.
* 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:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 5 * 10<sup>5</sup></code>
* <code>1 <= queries.length <= 10<sup>5</sup></code>
* `queries[i].length == 3`
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code>
* <code>1 <= val<sub>i</sub> <= 5</code>
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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:**

* <code>2 <= nums.length <= 10<sup>5</sup></code>
* `nums[i]` is either -1 or in the range <code>[1, 10<sup>9</sup>]</code>.
Loading