|
| 1 | +<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>]</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><strong> </strong>1.</li> |
| 7 | + <li>The amount by which the value is decremented 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>maximum </strong>number of elements that can be removed from <code>queries</code>, such that <code>nums</code> can still be converted to a <strong>zero array</strong> using the <em>remaining</em> queries. If it is not possible to convert <code>nums</code> to a <strong>zero array</strong>, 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],[0,2],[1,1]]</span></p> |
| 19 | + |
| 20 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 21 | + |
| 22 | +<p><strong>Explanation:</strong></p> |
| 23 | + |
| 24 | +<p>After removing <code>queries[2]</code>, <code>nums</code> can still be converted to a zero array.</p> |
| 25 | + |
| 26 | +<ul> |
| 27 | + <li>Using <code>queries[0]</code>, decrement <code>nums[0]</code> and <code>nums[2]</code> by 1 and <code>nums[1]</code> by 0.</li> |
| 28 | + <li>Using <code>queries[1]</code>, decrement <code>nums[0]</code> and <code>nums[2]</code> by 1 and <code>nums[1]</code> by 0.</li> |
| 29 | +</ul> |
| 30 | +</div> |
| 31 | + |
| 32 | +<p><strong class="example">Example 2:</strong></p> |
| 33 | + |
| 34 | +<div class="example-block"> |
| 35 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1], queries = [[1,3],[0,2],[1,3],[1,2]]</span></p> |
| 36 | + |
| 37 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 38 | + |
| 39 | +<p><strong>Explanation:</strong></p> |
| 40 | + |
| 41 | +<p>We can remove <code>queries[2]</code> and <code>queries[3]</code>.</p> |
| 42 | +</div> |
| 43 | + |
| 44 | +<p><strong class="example">Example 3:</strong></p> |
| 45 | + |
| 46 | +<div class="example-block"> |
| 47 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], queries = [[0,3]]</span></p> |
| 48 | + |
| 49 | +<p><strong>Output:</strong> <span class="example-io">-1</span></p> |
| 50 | + |
| 51 | +<p><strong>Explanation:</strong></p> |
| 52 | + |
| 53 | +<p><code>nums</code> cannot be converted to a zero array even after using all the queries.</p> |
| 54 | +</div> |
| 55 | + |
| 56 | +<p> </p> |
| 57 | +<p><strong>Constraints:</strong></p> |
| 58 | + |
| 59 | +<ul> |
| 60 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 61 | + <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li> |
| 62 | + <li><code>1 <= queries.length <= 10<sup>5</sup></code></li> |
| 63 | + <li><code>queries[i].length == 2</code></li> |
| 64 | + <li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code></li> |
| 65 | +</ul> |
0 commit comments