|
1 | 1 | # [3542.Minimum Operations to Convert All Elements to Zero][title] |
2 | 2 |
|
3 | | -> [!WARNING|style:flat] |
4 | | -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) |
5 | | -
|
6 | 3 | ## Description |
| 4 | +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. |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +Return the **minimum** number of operations required to make all elements in the array 0. |
7 | 9 |
|
8 | 10 | **Example 1:** |
9 | 11 |
|
10 | 12 | ``` |
11 | | -Input: a = "11", b = "1" |
12 | | -Output: "100" |
| 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. |
13 | 21 | ``` |
14 | 22 |
|
15 | | -## 题意 |
16 | | -> ... |
| 23 | +**Example 2:** |
| 24 | + |
| 25 | +``` |
| 26 | +Input: nums = [3,1,2,1] |
17 | 27 |
|
18 | | -## 题解 |
| 28 | +Output: 3 |
| 29 | +
|
| 30 | +Explanation: |
| 31 | +
|
| 32 | +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]. |
| 33 | +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]. |
| 34 | +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]. |
| 35 | +Thus, the minimum number of operations required is 3. |
| 36 | +``` |
| 37 | + |
| 38 | +**Example 3:** |
19 | 39 |
|
20 | | -### 思路1 |
21 | | -> ... |
22 | | -Minimum Operations to Convert All Elements to Zero |
23 | | -```go |
24 | 40 | ``` |
| 41 | +Input: nums = [1,2,1,2,1,2] |
25 | 42 |
|
| 43 | +Output: 4 |
| 44 | +
|
| 45 | +Explanation: |
| 46 | +
|
| 47 | +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]. |
| 48 | +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]. |
| 49 | +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]. |
| 50 | +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]. |
| 51 | +Thus, the minimum number of operations required is 4. |
| 52 | +``` |
26 | 53 |
|
27 | 54 | ## 结语 |
28 | 55 |
|
|
0 commit comments