|
1 | 1 | # [3354.Make Array Elements Equal 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 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. |
7 | 21 |
|
8 | 22 | **Example 1:** |
9 | 23 |
|
10 | 24 | ``` |
11 | | -Input: a = "11", b = "1" |
12 | | -Output: "100" |
13 | | -``` |
| 25 | +Input: nums = [1,0,2,0,3] |
14 | 26 |
|
15 | | -## 题意 |
16 | | -> ... |
| 27 | +Output: 2 |
17 | 28 |
|
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:** |
19 | 40 |
|
20 | | -### 思路1 |
21 | | -> ... |
22 | | -Make Array Elements Equal to Zero |
23 | | -```go |
24 | 41 | ``` |
| 42 | +Input: nums = [2,3,4,0,4,1,0] |
25 | 43 |
|
| 44 | +Output: 0 |
| 45 | +
|
| 46 | +Explanation: |
| 47 | +
|
| 48 | +There are no possible valid selections. |
| 49 | +``` |
26 | 50 |
|
27 | 51 | ## 结语 |
28 | 52 |
|
|
0 commit comments