|
| 1 | +3489\. Zero Array Transformation IV |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +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>. |
| 6 | + |
| 7 | +Each `queries[i]` represents the following action on `nums`: |
| 8 | + |
| 9 | +* Select a **subset** of indices in the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> from `nums`. |
| 10 | +* Decrement the value at each selected index by **exactly** <code>val<sub>i</sub></code>. |
| 11 | + |
| 12 | +A **Zero Array** is an array with all its elements equal to 0. |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +**Input:** nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]] |
| 19 | + |
| 20 | +**Output:** 2 |
| 21 | + |
| 22 | +**Explanation:** |
| 23 | + |
| 24 | +* **For query 0 (l = 0, r = 2, val = 1):** |
| 25 | + * Decrement the values at indices `[0, 2]` by 1. |
| 26 | + * The array will become `[1, 0, 1]`. |
| 27 | +* **For query 1 (l = 0, r = 2, val = 1):** |
| 28 | + * Decrement the values at indices `[0, 2]` by 1. |
| 29 | + * The array will become `[0, 0, 0]`, which is a Zero Array. Therefore, the minimum value of `k` is 2. |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | +**Input:** nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]] |
| 34 | + |
| 35 | +**Output:** \-1 |
| 36 | + |
| 37 | +**Explanation:** |
| 38 | + |
| 39 | +It is impossible to make nums a Zero Array even after all the queries. |
| 40 | + |
| 41 | +**Example 3:** |
| 42 | + |
| 43 | +**Input:** nums = [1,2,3,2,1], queries = [[0,1,1],[1,2,1],[2,3,2],[3,4,1],[4,4,1]] |
| 44 | + |
| 45 | +**Output:** 4 |
| 46 | + |
| 47 | +**Explanation:** |
| 48 | + |
| 49 | +* **For query 0 (l = 0, r = 1, val = 1):** |
| 50 | + * Decrement the values at indices `[0, 1]` by `1`. |
| 51 | + * The array will become `[0, 1, 3, 2, 1]`. |
| 52 | +* **For query 1 (l = 1, r = 2, val = 1):** |
| 53 | + * Decrement the values at indices `[1, 2]` by 1. |
| 54 | + * The array will become `[0, 0, 2, 2, 1]`. |
| 55 | +* **For query 2 (l = 2, r = 3, val = 2):** |
| 56 | + * Decrement the values at indices `[2, 3]` by 2. |
| 57 | + * The array will become `[0, 0, 0, 0, 1]`. |
| 58 | +* **For query 3 (l = 3, r = 4, val = 1):** |
| 59 | + * Decrement the value at index 4 by 1. |
| 60 | + * The array will become `[0, 0, 0, 0, 0]`. Therefore, the minimum value of `k` is 4. |
| 61 | + |
| 62 | +**Example 4:** |
| 63 | + |
| 64 | +**Input:** nums = [1,2,3,2,6], queries = [[0,1,1],[0,2,1],[1,4,2],[4,4,4],[3,4,1],[4,4,5]] |
| 65 | + |
| 66 | +**Output:** 4 |
| 67 | + |
| 68 | +**Constraints:** |
| 69 | + |
| 70 | +* `1 <= nums.length <= 10` |
| 71 | +* `0 <= nums[i] <= 1000` |
| 72 | +* `1 <= queries.length <= 1000` |
| 73 | +* <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>, val<sub>i</sub>]</code> |
| 74 | +* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code> |
| 75 | +* <code>1 <= val<sub>i</sub> <= 10</code> |
0 commit comments