Skip to content

Commit 26c318e

Browse files
authored
Added tasks 3354-3357
1 parent 1e6947e commit 26c318e

File tree

5 files changed

+376
-0
lines changed
  • src/main/java/g3301_3400
    • s3354_make_array_elements_equal_to_zero
    • s3355_zero_array_transformation_i
    • s3356_zero_array_transformation_ii
    • s3357_minimize_the_maximum_adjacent_element_difference

5 files changed

+376
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,10 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3357 |[Minimize the Maximum Adjacent Element Difference](src/main/java/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference)| Hard | Array, Greedy, Binary_Search | 5 | 100.00
1820+
| 3356 |[Zero Array Transformation II](src/main/java/g3301_3400/s3356_zero_array_transformation_ii)| Medium | Array, Binary_Search, Prefix_Sum | 4 | 93.46
1821+
| 3355 |[Zero Array Transformation I](src/main/java/g3301_3400/s3355_zero_array_transformation_i)| Medium | Array, Prefix_Sum | 3 | 91.34
1822+
| 3354 |[Make Array Elements Equal to Zero](src/main/java/g3301_3400/s3354_make_array_elements_equal_to_zero)| Easy | Array, Simulation, Prefix_Sum | 1 | 95.09
18191823
| 3352 |[Count K-Reducible Numbers Less Than N](src/main/java/g3301_3400/s3352_count_k_reducible_numbers_less_than_n)| Hard | String, Dynamic_Programming, Math, Combinatorics | 11 | 99.58
18201824
| 3351 |[Sum of Good Subsequences](src/main/java/g3301_3400/s3351_sum_of_good_subsequences)| Hard | Array, Hash_Table, Dynamic_Programming | 13 | 99.09
18211825
| 3350 |[Adjacent Increasing Subarrays Detection II](src/main/java/g3301_3400/s3350_adjacent_increasing_subarrays_detection_ii)| Medium | Array, Binary_Search | 10 | 99.76
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3354\. Make Array Elements Equal to Zero
5+
6+
Easy
7+
8+
You are given an integer array `nums`.
9+
10+
Start by selecting a starting position `curr` such that `nums[curr] == 0`, and choose a movement **direction** of either left or right.
11+
12+
After that, you repeat the following process:
13+
14+
* If `curr` is out of the range `[0, n - 1]`, this process ends.
15+
* 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.
16+
* Else if `nums[curr] > 0`:
17+
* Decrement `nums[curr]` by 1.
18+
* **Reverse** your movement direction (left becomes right and vice versa).
19+
* Take a step in your new direction.
20+
21+
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.
22+
23+
Return the number of possible **valid** selections.
24+
25+
**Example 1:**
26+
27+
**Input:** nums = [1,0,2,0,3]
28+
29+
**Output:** 2
30+
31+
**Explanation:**
32+
33+
The only possible valid selections are the following:
34+
35+
* Choose `curr = 3`, and a movement direction to the left.
36+
* <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>.
37+
* Choose `curr = 3`, and a movement direction to the right.
38+
* <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>
39+
40+
**Example 2:**
41+
42+
**Input:** nums = [2,3,4,0,4,1,0]
43+
44+
**Output:** 0
45+
46+
**Explanation:**
47+
48+
There are no possible valid selections.
49+
50+
**Constraints:**
51+
52+
* `1 <= nums.length <= 100`
53+
* `0 <= nums[i] <= 100`
54+
* There is at least one element `i` where `nums[i] == 0`.
55+
56+
## Solution
57+
58+
```java
59+
public class Solution {
60+
public int countValidSelections(int[] nums) {
61+
int[] rightSum = new int[nums.length];
62+
int[] leftSum = new int[nums.length];
63+
int result = 0;
64+
leftSum[0] = 0;
65+
rightSum[nums.length - 1] = 0;
66+
for (int i = 1; i < nums.length; i++) {
67+
leftSum[i] = leftSum[i - 1] + nums[i - 1];
68+
}
69+
for (int j = nums.length - 2; j >= 0; j--) {
70+
rightSum[j] = rightSum[j + 1] + nums[j + 1];
71+
}
72+
for (int k = 0; k < nums.length; k++) {
73+
if (nums[k] == 0 && Math.abs(rightSum[k] - leftSum[k]) == 1) {
74+
result++;
75+
}
76+
if (nums[k] == 0 && Math.abs(rightSum[k] - leftSum[k]) == 0) {
77+
result += 2;
78+
}
79+
}
80+
return result;
81+
}
82+
}
83+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3355\. Zero Array Transformation I
5+
6+
Medium
7+
8+
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>.
9+
10+
For each `queries[i]`:
11+
12+
* Select a subset of indices within the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in `nums`.
13+
* Decrement the values at the selected indices by 1.
14+
15+
A **Zero Array** is an array where all elements are equal to 0.
16+
17+
Return `true` if it is _possible_ to transform `nums` into a **Zero Array** after processing all the queries sequentially, otherwise return `false`.
18+
19+
A **subset** of an array is a selection of elements (possibly none) of the array.
20+
21+
**Example 1:**
22+
23+
**Input:** nums = [1,0,1], queries = \[\[0,2]]
24+
25+
**Output:** true
26+
27+
**Explanation:**
28+
29+
* **For i = 0:**
30+
* Select the subset of indices as `[0, 2]` and decrement the values at these indices by 1.
31+
* The array will become `[0, 0, 0]`, which is a Zero Array.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [4,3,2,1], queries = \[\[1,3],[0,2]]
36+
37+
**Output:** false
38+
39+
**Explanation:**
40+
41+
* **For i = 0:**
42+
* Select the subset of indices as `[1, 2, 3]` and decrement the values at these indices by 1.
43+
* The array will become `[4, 2, 1, 0]`.
44+
* **For i = 1:**
45+
* Select the subset of indices as `[0, 1, 2]` and decrement the values at these indices by 1.
46+
* The array will become `[3, 1, 0, 0]`, which is not a Zero Array.
47+
48+
**Constraints:**
49+
50+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
51+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
52+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
53+
* `queries[i].length == 2`
54+
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code>
55+
56+
## Solution
57+
58+
```java
59+
public class Solution {
60+
public boolean isZeroArray(int[] nums, int[][] queries) {
61+
int n = nums.length;
62+
int sum = 0;
63+
for (int num : nums) {
64+
sum += num;
65+
}
66+
if (sum == 0) {
67+
return true;
68+
}
69+
int[] diff = new int[n + 1];
70+
for (int[] q : queries) {
71+
int low = q[0];
72+
int high = q[1];
73+
diff[low] -= 1;
74+
if (high + 1 < n) {
75+
diff[high + 1] += 1;
76+
}
77+
}
78+
for (int i = 0; i < n; i++) {
79+
if (i > 0) {
80+
diff[i] += diff[i - 1];
81+
}
82+
nums[i] += diff[i];
83+
sum += diff[i];
84+
if (nums[i] > 0) {
85+
return false;
86+
}
87+
}
88+
return sum <= 0;
89+
}
90+
}
91+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3356\. Zero Array Transformation II
5+
6+
Medium
7+
8+
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>.
9+
10+
Each `queries[i]` represents the following action on `nums`:
11+
12+
* 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>.
13+
* The amount by which each value is decremented can be chosen **independently** for each index.
14+
15+
A **Zero Array** is an array with all its elements equal to 0.
16+
17+
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.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [2,0,2], queries = \[\[0,2,1],[0,2,1],[1,1,3]]
22+
23+
**Output:** 2
24+
25+
**Explanation:**
26+
27+
* **For i = 0 (l = 0, r = 2, val = 1):**
28+
* Decrement values at indices `[0, 1, 2]` by `[1, 0, 1]` respectively.
29+
* The array will become `[1, 0, 1]`.
30+
* **For i = 1 (l = 0, r = 2, val = 1):**
31+
* Decrement values at indices `[0, 1, 2]` by `[1, 0, 1]` respectively.
32+
* The array will become `[0, 0, 0]`, which is a Zero Array. Therefore, the minimum value of `k` is 2.
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [4,3,2,1], queries = \[\[1,3,2],[0,2,1]]
37+
38+
**Output:** \-1
39+
40+
**Explanation:**
41+
42+
* **For i = 0 (l = 1, r = 3, val = 2):**
43+
* Decrement values at indices `[1, 2, 3]` by `[2, 2, 1]` respectively.
44+
* The array will become `[4, 1, 0, 0]`.
45+
* **For i = 1 (l = 0, r = 2, val \= 1):**
46+
* Decrement values at indices `[0, 1, 2]` by `[1, 1, 0]` respectively.
47+
* The array will become `[3, 0, 0, 0]`, which is not a Zero Array.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
52+
* <code>0 <= nums[i] <= 5 * 10<sup>5</sup></code>
53+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
54+
* `queries[i].length == 3`
55+
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code>
56+
* <code>1 <= val<sub>i</sub> <= 5</code>
57+
58+
## Solution
59+
60+
```java
61+
public class Solution {
62+
public int minZeroArray(int[] nums, int[][] queries) {
63+
int[] diff = new int[nums.length];
64+
int idx = 0;
65+
int d = 0;
66+
for (int i = 0; i < nums.length; i++) {
67+
d += diff[i];
68+
while (nums[i] + d > 0 && idx < queries.length) {
69+
int[] q = queries[idx];
70+
if (i >= q[0] && i <= q[1]) {
71+
d -= q[2];
72+
}
73+
diff[q[0]] -= q[2];
74+
if (q[1] + 1 < nums.length) {
75+
diff[q[1] + 1] += q[2];
76+
}
77+
idx++;
78+
}
79+
if (nums[i] + d > 0) {
80+
return -1;
81+
}
82+
}
83+
return idx;
84+
}
85+
}
86+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3357\. Minimize the Maximum Adjacent Element Difference
5+
6+
Hard
7+
8+
You are given an array of integers `nums`. Some values in `nums` are **missing** and are denoted by -1.
9+
10+
You can choose a pair of **positive** integers `(x, y)` **exactly once** and replace each **missing** element with _either_ `x` or `y`.
11+
12+
You need to **minimize** the **maximum** **absolute difference** between _adjacent_ elements of `nums` after replacements.
13+
14+
Return the **minimum** possible difference.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,-1,10,8]
19+
20+
**Output:** 4
21+
22+
**Explanation:**
23+
24+
By choosing the pair as `(6, 7)`, nums can be changed to `[1, 2, 6, 10, 8]`.
25+
26+
The absolute differences between adjacent elements are:
27+
28+
* `|1 - 2| == 1`
29+
* `|2 - 6| == 4`
30+
* `|6 - 10| == 4`
31+
* `|10 - 8| == 2`
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [-1,-1,-1]
36+
37+
**Output:** 0
38+
39+
**Explanation:**
40+
41+
By choosing the pair as `(4, 4)`, nums can be changed to `[4, 4, 4]`.
42+
43+
**Example 3:**
44+
45+
**Input:** nums = [-1,10,-1,8]
46+
47+
**Output:** 1
48+
49+
**Explanation:**
50+
51+
By choosing the pair as `(11, 9)`, nums can be changed to `[11, 10, 9, 8]`.
52+
53+
**Constraints:**
54+
55+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
56+
* `nums[i]` is either -1 or in the range <code>[1, 10<sup>9</sup>]</code>.
57+
58+
## Solution
59+
60+
```java
61+
public class Solution {
62+
public int minDifference(int[] nums) {
63+
int n = nums.length;
64+
int maxAdj = 0;
65+
int mina = Integer.MAX_VALUE;
66+
int maxb = Integer.MIN_VALUE;
67+
for (int i = 0; i < n - 1; ++i) {
68+
int a = nums[i];
69+
int b = nums[i + 1];
70+
if (a > 0 && b > 0) {
71+
maxAdj = Math.max(maxAdj, Math.abs(a - b));
72+
} else if (a > 0 || b > 0) {
73+
mina = Math.min(mina, Math.max(a, b));
74+
maxb = Math.max(maxb, Math.max(a, b));
75+
}
76+
}
77+
int res = 0;
78+
for (int i = 0; i < n; ++i) {
79+
if ((i > 0 && nums[i - 1] == -1) || nums[i] > 0) {
80+
continue;
81+
}
82+
int j = i;
83+
while (j < n && nums[j] == -1) {
84+
j++;
85+
}
86+
int a = Integer.MAX_VALUE;
87+
int b = Integer.MIN_VALUE;
88+
if (i > 0) {
89+
a = Math.min(a, nums[i - 1]);
90+
b = Math.max(b, nums[i - 1]);
91+
}
92+
if (j < n) {
93+
a = Math.min(a, nums[j]);
94+
b = Math.max(b, nums[j]);
95+
}
96+
if (a <= b) {
97+
if (j - i == 1) {
98+
res = Math.max(res, Math.min(maxb - a, b - mina));
99+
} else {
100+
res =
101+
Math.max(
102+
res,
103+
Math.min(
104+
maxb - a,
105+
Math.min(b - mina, (maxb - mina + 2) / 3 * 2)));
106+
}
107+
}
108+
}
109+
return Math.max(maxAdj, (res + 1) / 2);
110+
}
111+
}
112+
```

0 commit comments

Comments
 (0)