Skip to content

Commit 8716858

Browse files
committed
Add solution and test-cases for problem 3354
1 parent f03226b commit 8716858

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
11
# [3354.Make Array Elements Equal 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 integer array `nums`.
5+
6+
Start by selecting a starting position `curr` such that `nums[curr] == 0`, and choose a movement **direction** of either left or right.
7+
8+
After that, you repeat the following process:
9+
10+
- If `curr` is out of the range `[0, n - 1]`, this process ends.
11+
- If `nums[curr] == 0`, move in the current direction by **incrementing** `curr` if you are moving right, or **decrementing** `curr` if you are moving left.
12+
- Else if `nums[curr] > 0`:
13+
14+
- Decrement `nums[curr]` by 1.
15+
- **Reverse** your movement direction (left becomes right and vice versa).
16+
- Take a step in your new direction.
17+
18+
A selection of the initial position `curr` and movement direction is considered **valid** if every element in `nums` becomes 0 by the end of the process.
19+
20+
Return the number of possible **valid** selections.
721

822
**Example 1:**
923

1024
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
25+
Input: nums = [1,0,2,0,3]
1426
15-
## 题意
16-
> ...
27+
Output: 2
1728
18-
## 题解
29+
Explanation:
30+
31+
The only possible valid selections are the following:
32+
33+
Choose curr = 3, and a movement direction to the left.
34+
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,0].
35+
Choose curr = 3, and a movement direction to the right.
36+
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0].
37+
```
38+
39+
**Example 2:**
1940

20-
### 思路1
21-
> ...
22-
Make Array Elements Equal to Zero
23-
```go
2441
```
42+
Input: nums = [2,3,4,0,4,1,0]
2543
44+
Output: 0
45+
46+
Explanation:
47+
48+
There are no possible valid selections.
49+
```
2650

2751
## 结语
2852

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
l := len(nums)
5+
left, right := make([]int, l+2), make([]int, l+2)
6+
for i := 1; i <= l; i++ {
7+
left[i] = left[i-1] + nums[i-1]
8+
}
9+
for i := l; i >= 1; i-- {
10+
right[i] = right[i+1] + nums[i-1]
11+
}
12+
var ret int
13+
for index := 1; index <= l; index++ {
14+
if nums[index-1] != 0 {
15+
continue
16+
}
17+
diff := left[index-1] - right[index+1]
18+
if diff == 0 {
19+
ret += 2
20+
}
21+
if diff == 1 || diff == -1 {
22+
ret++
23+
}
24+
}
25+
return ret
526
}

leetcode/3301-3400/3354.Make-Array-Elements-Equal-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{1, 0, 2, 0, 3}, 2},
17+
{"TestCase2", []int{2, 3, 4, 0, 4, 1, 0}, 0},
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)