|
| 1 | +3542\. Minimum Operations to Convert All Elements to Zero |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +You are given an array `nums` of size `n`, consisting of **non-negative** integers. Your task is to apply some (possibly zero) operations on the array so that **all** elements become 0. |
| 6 | + |
| 7 | +In one operation, you can select a subarray `[i, j]` (where `0 <= i <= j < n`) and set all occurrences of the **minimum** **non-negative** integer in that subarray to 0. |
| 8 | + |
| 9 | +Return the **minimum** number of operations required to make all elements in the array 0. |
| 10 | + |
| 11 | +**Example 1:** |
| 12 | + |
| 13 | +**Input:** nums = [0,2] |
| 14 | + |
| 15 | +**Output:** 1 |
| 16 | + |
| 17 | +**Explanation:** |
| 18 | + |
| 19 | +* Select the subarray `[1,1]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[0,0]`. |
| 20 | +* Thus, the minimum number of operations required is 1. |
| 21 | + |
| 22 | +**Example 2:** |
| 23 | + |
| 24 | +**Input:** nums = [3,1,2,1] |
| 25 | + |
| 26 | +**Output:** 3 |
| 27 | + |
| 28 | +**Explanation:** |
| 29 | + |
| 30 | +* Select subarray `[1,3]` (which is `[1,2,1]`), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in `[3,0,2,0]`. |
| 31 | +* Select subarray `[2,2]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[3,0,0,0]`. |
| 32 | +* Select subarray `[0,0]` (which is `[3]`), where the minimum non-negative integer is 3. Setting all occurrences of 3 to 0 results in `[0,0,0,0]`. |
| 33 | +* Thus, the minimum number of operations required is 3. |
| 34 | + |
| 35 | +**Example 3:** |
| 36 | + |
| 37 | +**Input:** nums = [1,2,1,2,1,2] |
| 38 | + |
| 39 | +**Output:** 4 |
| 40 | + |
| 41 | +**Explanation:** |
| 42 | + |
| 43 | +* Select subarray `[0,5]` (which is `[1,2,1,2,1,2]`), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in `[0,2,0,2,0,2]`. |
| 44 | +* Select subarray `[1,1]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[0,0,0,2,0,2]`. |
| 45 | +* Select subarray `[3,3]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[0,0,0,0,0,2]`. |
| 46 | +* Select subarray `[5,5]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[0,0,0,0,0,0]`. |
| 47 | +* Thus, the minimum number of operations required is 4. |
| 48 | + |
| 49 | +**Constraints:** |
| 50 | + |
| 51 | +* <code>1 <= n == nums.length <= 10<sup>5</sup></code> |
| 52 | +* <code>0 <= nums[i] <= 10<sup>5</sup></code> |
0 commit comments