|
| 1 | +<h2><a href="https://leetcode.com/problems/zero-array-transformation-ii/?envType=daily-question&envId=2025-03-13">3356. Zero Array Transformation II</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>nums</code> of length <code>n</code> and a 2D array <code>queries</code> where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>, val<sub>i</sub>]</code>.</p> |
| 2 | + |
| 3 | +<p>Each <code>queries[i]</code> represents the following action on <code>nums</code>:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Decrement the value at each index in the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in <code>nums</code> by <strong>at most</strong> <code>val<sub>i</sub></code>.</li> |
| 7 | + <li>The amount by which each value is decremented<!-- notionvc: b232c9d9-a32d-448c-85b8-b637de593c11 --> can be chosen <strong>independently</strong> for each index.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>A <strong>Zero Array</strong> is an array with all its elements equal to 0.</p> |
| 11 | + |
| 12 | +<p>Return the <strong>minimum</strong> possible <strong>non-negative</strong> value of <code>k</code>, such that after processing the first <code>k</code> queries in <strong>sequence</strong>, <code>nums</code> becomes a <strong>Zero Array</strong>. If no such <code>k</code> exists, return -1.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<div class="example-block"> |
| 18 | +<p><strong>Input:</strong> <span class="example-io">nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]</span></p> |
| 19 | + |
| 20 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 21 | + |
| 22 | +<p><strong>Explanation:</strong></p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li><strong>For i = 0 (l = 0, r = 2, val = 1):</strong> |
| 26 | + |
| 27 | + <ul> |
| 28 | + <li>Decrement values at indices <code>[0, 1, 2]</code> by <code>[1, 0, 1]</code> respectively.</li> |
| 29 | + <li>The array will become <code>[1, 0, 1]</code>.</li> |
| 30 | + </ul> |
| 31 | + </li> |
| 32 | + <li><strong>For i = 1 (l = 0, r = 2, val = 1):</strong> |
| 33 | + <ul> |
| 34 | + <li>Decrement values at indices <code>[0, 1, 2]</code> by <code>[1, 0, 1]</code> respectively.</li> |
| 35 | + <li>The array will become <code>[0, 0, 0]</code>, which is a Zero Array. Therefore, the minimum value of <code>k</code> is 2.</li> |
| 36 | + </ul> |
| 37 | + </li> |
| 38 | +</ul> |
| 39 | +</div> |
| 40 | + |
| 41 | +<p><strong class="example">Example 2:</strong></p> |
| 42 | + |
| 43 | +<div class="example-block"> |
| 44 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]]</span></p> |
| 45 | + |
| 46 | +<p><strong>Output:</strong> <span class="example-io">-1</span></p> |
| 47 | + |
| 48 | +<p><strong>Explanation:</strong></p> |
| 49 | + |
| 50 | +<ul> |
| 51 | + <li><strong>For i = 0 (l = 1, r = 3, val = 2):</strong> |
| 52 | + |
| 53 | + <ul> |
| 54 | + <li>Decrement values at indices <code>[1, 2, 3]</code> by <code>[2, 2, 1]</code> respectively.</li> |
| 55 | + <li>The array will become <code>[4, 1, 0, 0]</code>.</li> |
| 56 | + </ul> |
| 57 | + </li> |
| 58 | + <li><strong>For i = 1 (l = 0, r = 2, val<span style="font-size: 13.3333px;"> </span>= 1):</strong> |
| 59 | + <ul> |
| 60 | + <li>Decrement values at indices <code>[0, 1, 2]</code> by <code>[1, 1, 0]</code> respectively.</li> |
| 61 | + <li>The array will become <code>[3, 0, 0, 0]</code>, which is not a Zero Array.</li> |
| 62 | + </ul> |
| 63 | + </li> |
| 64 | +</ul> |
| 65 | +</div> |
| 66 | + |
| 67 | +<p> </p> |
| 68 | +<p><strong>Constraints:</strong></p> |
| 69 | + |
| 70 | +<ul> |
| 71 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 72 | + <li><code>0 <= nums[i] <= 5 * 10<sup>5</sup></code></li> |
| 73 | + <li><code>1 <= queries.length <= 10<sup>5</sup></code></li> |
| 74 | + <li><code>queries[i].length == 3</code></li> |
| 75 | + <li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code></li> |
| 76 | + <li><code>1 <= val<sub>i</sub> <= 5</code></li> |
| 77 | +</ul> |
0 commit comments