|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-operations-to-convert-all-elements-to-zero">3834. Minimum Operations to Convert All Elements to Zero</a></h2><h3>Medium</h3><hr><p>You are given an array <code>nums</code> of size <code>n</code>, consisting of <strong>non-negative</strong> integers. Your task is to apply some (possibly zero) operations on the array so that <strong>all</strong> elements become 0.</p> |
| 2 | + |
| 3 | +<p>In one operation, you can select a <span data-keyword="subarray">subarray</span> <code>[i, j]</code> (where <code>0 <= i <= j < n</code>) and set all occurrences of the <strong>minimum</strong> <strong>non-negative</strong> integer in that subarray to 0.</p> |
| 4 | + |
| 5 | +<p>Return the <strong>minimum</strong> number of operations required to make all elements in the array 0.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<div class="example-block"> |
| 11 | +<p><strong>Input:</strong> <span class="example-io">nums = [0,2]</span></p> |
| 12 | + |
| 13 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 14 | + |
| 15 | +<p><strong>Explanation:</strong></p> |
| 16 | + |
| 17 | +<ul> |
| 18 | + <li>Select the subarray <code>[1,1]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[0,0]</code>.</li> |
| 19 | + <li>Thus, the minimum number of operations required is 1.</li> |
| 20 | +</ul> |
| 21 | +</div> |
| 22 | + |
| 23 | +<p><strong class="example">Example 2:</strong></p> |
| 24 | + |
| 25 | +<div class="example-block"> |
| 26 | +<p><strong>Input:</strong> <span class="example-io">nums = [3,1,2,1]</span></p> |
| 27 | + |
| 28 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 29 | + |
| 30 | +<p><strong>Explanation:</strong></p> |
| 31 | + |
| 32 | +<ul> |
| 33 | + <li>Select subarray <code>[1,3]</code> (which is <code>[1,2,1]</code>), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in <code>[3,0,2,0]</code>.</li> |
| 34 | + <li>Select subarray <code>[2,2]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[3,0,0,0]</code>.</li> |
| 35 | + <li>Select subarray <code>[0,0]</code> (which is <code>[3]</code>), where the minimum non-negative integer is 3. Setting all occurrences of 3 to 0 results in <code>[0,0,0,0]</code>.</li> |
| 36 | + <li>Thus, the minimum number of operations required is 3.</li> |
| 37 | +</ul> |
| 38 | +</div> |
| 39 | + |
| 40 | +<p><strong class="example">Example 3:</strong></p> |
| 41 | + |
| 42 | +<div class="example-block"> |
| 43 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,2,1,2]</span></p> |
| 44 | + |
| 45 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 46 | + |
| 47 | +<p><strong>Explanation:</strong></p> |
| 48 | + |
| 49 | +<ul> |
| 50 | + <li>Select subarray <code>[0,5]</code> (which is <code>[1,2,1,2,1,2]</code>), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in <code>[0,2,0,2,0,2]</code>.</li> |
| 51 | + <li>Select subarray <code>[1,1]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[0,0,0,2,0,2]</code>.</li> |
| 52 | + <li>Select subarray <code>[3,3]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[0,0,0,0,0,2]</code>.</li> |
| 53 | + <li>Select subarray <code>[5,5]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[0,0,0,0,0,0]</code>.</li> |
| 54 | + <li>Thus, the minimum number of operations required is 4.</li> |
| 55 | +</ul> |
| 56 | +</div> |
| 57 | + |
| 58 | +<p> </p> |
| 59 | +<p><strong>Constraints:</strong></p> |
| 60 | + |
| 61 | +<ul> |
| 62 | + <li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li> |
| 63 | + <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li> |
| 64 | +</ul> |
0 commit comments