Skip to content

Added tasks 3354-3357 #1875

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 6 commits 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,28 @@
package g3301_3400.s3354_make_array_elements_equal_to_zero;

// #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) {
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;
}
}
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_3_ms_(91.34%)_Space_96_MB_(17.22%)

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;
}
}
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_4_ms_(93.46%)_Space_118.5_MB_(13.87%)

public class Solution {
public int minZeroArray(int[] nums, int[][] queries) {
int[] diff = new int[nums.length];
int idx = 0;
int d = 0;
for (int i = 0; i < nums.length; i++) {
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;
}
}
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,55 @@
package g3301_3400.s3357_minimize_the_maximum_adjacent_element_difference;

// #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) {
int n = nums.length;
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) {
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));
}
}
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;
int 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(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>.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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));
}

@Test
void countValidSelections3() {
assertThat(
new Solution()
.countValidSelections(new int[] {16, 13, 10, 0, 0, 0, 10, 6, 7, 8, 7}),
equalTo(3));
}
}
Loading