Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
# [3354.Make Array Elements Equal to Zero][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given an integer array `nums`.

Start by selecting a starting position `curr` such that `nums[curr] == 0`, and choose a movement **direction** of either left or right.

After that, you repeat the following process:

- If `curr` is out of the range `[0, n - 1]`, this process ends.
- 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.
- Else if `nums[curr] > 0`:

- Decrement `nums[curr]` by 1.
- **Reverse** your movement direction (left becomes right and vice versa).
- Take a step in your new direction.

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.

Return the number of possible **valid** selections.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: nums = [1,0,2,0,3]

## 题意
> ...
Output: 2

## 题解
Explanation:

The only possible valid selections are the following:

Choose curr = 3, and a movement direction to the left.
[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].
Choose curr = 3, and a movement direction to the right.
[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].
```

**Example 2:**

### 思路1
> ...
Make Array Elements Equal to Zero
```go
```
Input: nums = [2,3,4,0,4,1,0]

Output: 0

Explanation:

There are no possible valid selections.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
l := len(nums)
left, right := make([]int, l+2), make([]int, l+2)
for i := 1; i <= l; i++ {
left[i] = left[i-1] + nums[i-1]
}
for i := l; i >= 1; i-- {
right[i] = right[i+1] + nums[i-1]
}
var ret int
for index := 1; index <= l; index++ {
if nums[index-1] != 0 {
continue
}
diff := left[index-1] - right[index+1]
if diff == 0 {
ret += 2
}
if diff == 1 || diff == -1 {
ret++
}
}
return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 0, 2, 0, 3}, 2},
{"TestCase2", []int{2, 3, 4, 0, 4, 1, 0}, 0},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading