Skip to content

Commit 562a0a8

Browse files
authored
Merge pull request #1368 from 0xff-dev/3542
Add solution and test-cases for problem 3542
2 parents 6624bc0 + 20ab8f0 commit 562a0a8

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/README.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
# [3542.Minimum Operations to Convert All Elements to Zero][title]
22

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-
63
## 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.
79

810
**Example 1:**
911

1012
```
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.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
24+
25+
```
26+
Input: nums = [3,1,2,1]
1727
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:**
1939

20-
### 思路1
21-
> ...
22-
Minimum Operations to Convert All Elements to Zero
23-
```go
2440
```
41+
Input: nums = [1,2,1,2,1,2]
2542
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+
```
2653

2754
## 结语
2855

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
s := []int{}
5+
res := 0
6+
for _, a := range nums {
7+
for len(s) > 0 && s[len(s)-1] > a {
8+
s = s[:len(s)-1]
9+
}
10+
if a == 0 {
11+
continue
12+
}
13+
if len(s) == 0 || s[len(s)-1] < a {
14+
res++
15+
s = append(s, a)
16+
}
17+
}
18+
return res
519
}

leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{0, 2}, 1},
17+
{"TestCase2", []int{3, 1, 2, 1}, 3},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)