|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3354. Make Array Elements Equal to Zero](https://leetcode.com/problems/make-array-elements-equal-to-zero) |
| 10 | + |
| 11 | +[中文文档](/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given an integer array <code>nums</code>.</p> |
| 18 | + |
| 19 | +<p>Start by selecting a starting position <code>curr</code> such that <code>nums[curr] == 0</code>, and choose a movement <strong>direction</strong> of either left or right.</p> |
| 20 | + |
| 21 | +<p>After that, you repeat the following process:</p> |
| 22 | + |
| 23 | +<ul> |
| 24 | + <li>If <code>curr</code> is out of the range <code>[0, n - 1]</code>, this process ends.</li> |
| 25 | + <li>If <code>nums[curr] == 0</code>, move in the current direction by <strong>incrementing</strong> <code>curr</code> if you are moving right, or <strong>decrementing</strong> <code>curr</code> if you are moving left.</li> |
| 26 | + <li>Else if <code>nums[curr] > 0</code>: |
| 27 | + <ul> |
| 28 | + <li>Decrement <code>nums[curr]</code> by 1.</li> |
| 29 | + <li><strong>Reverse</strong> your movement direction (left becomes right and vice versa).</li> |
| 30 | + <li>Take a step in your new direction.</li> |
| 31 | + </ul> |
| 32 | + </li> |
| 33 | +</ul> |
| 34 | + |
| 35 | +<p>A selection of the initial position <code>curr</code> and movement direction is considered <strong>valid</strong> if every element in <code>nums</code> becomes 0 by the end of the process.</p> |
| 36 | + |
| 37 | +<p>Return the number of possible <strong>valid</strong> selections.</p> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | +<p><strong class="example">Example 1:</strong></p> |
| 41 | + |
| 42 | +<div class="example-block"> |
| 43 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,0,2,0,3]</span></p> |
| 44 | + |
| 45 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 46 | + |
| 47 | +<p><strong>Explanation:</strong></p> |
| 48 | + |
| 49 | +<p>The only possible valid selections are the following:</p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li>Choose <code>curr = 3</code>, and a movement direction to the left. |
| 53 | + |
| 54 | + <ul> |
| 55 | + <li><code>[1,0,2,<strong><u>0</u></strong>,3] -> [1,0,<strong><u>2</u></strong>,0,3] -> [1,0,1,<strong><u>0</u></strong>,3] -> [1,0,1,0,<strong><u>3</u></strong>] -> [1,0,1,<strong><u>0</u></strong>,2] -> [1,0,<strong><u>1</u></strong>,0,2] -> [1,0,0,<strong><u>0</u></strong>,2] -> [1,0,0,0,<strong><u>2</u></strong>] -> [1,0,0,<strong><u>0</u></strong>,1] -> [1,0,<strong><u>0</u></strong>,0,1] -> [1,<strong><u>0</u></strong>,0,0,1] -> [<strong><u>1</u></strong>,0,0,0,1] -> [0,<strong><u>0</u></strong>,0,0,1] -> [0,0,<strong><u>0</u></strong>,0,1] -> [0,0,0,<strong><u>0</u></strong>,1] -> [0,0,0,0,<strong><u>1</u></strong>] -> [0,0,0,0,0]</code>.</li> |
| 56 | + </ul> |
| 57 | + </li> |
| 58 | + <li>Choose <code>curr = 3</code>, and a movement direction to the right. |
| 59 | + <ul> |
| 60 | + <li><code>[1,0,2,<strong><u>0</u></strong>,3] -> [1,0,2,0,<strong><u>3</u></strong>] -> [1,0,2,<strong><u>0</u></strong>,2] -> [1,0,<strong><u>2</u></strong>,0,2] -> [1,0,1,<strong><u>0</u></strong>,2] -> [1,0,1,0,<strong><u>2</u></strong>] -> [1,0,1,<strong><u>0</u></strong>,1] -> [1,0,<strong><u>1</u></strong>,0,1] -> [1,0,0,<strong><u>0</u></strong>,1] -> [1,0,0,0,<strong><u>1</u></strong>] -> [1,0,0,<strong><u>0</u></strong>,0] -> [1,0,<strong><u>0</u></strong>,0,0] -> [1,<strong><u>0</u></strong>,0,0,0] -> [<strong><u>1</u></strong>,0,0,0,0] -> [0,0,0,0,0].</code></li> |
| 61 | + </ul> |
| 62 | + </li> |
| 63 | + |
| 64 | +</ul> |
| 65 | +</div> |
| 66 | + |
| 67 | +<p><strong class="example">Example 2:</strong></p> |
| 68 | + |
| 69 | +<div class="example-block"> |
| 70 | +<p><strong>Input:</strong> <span class="example-io">nums = [2,3,4,0,4,1,0]</span></p> |
| 71 | + |
| 72 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 73 | + |
| 74 | +<p><strong>Explanation:</strong></p> |
| 75 | + |
| 76 | +<p>There are no possible valid selections.</p> |
| 77 | +</div> |
| 78 | + |
| 79 | +<p> </p> |
| 80 | +<p><strong>Constraints:</strong></p> |
| 81 | + |
| 82 | +<ul> |
| 83 | + <li><code>1 <= nums.length <= 100</code></li> |
| 84 | + <li><code>0 <= nums[i] <= 100</code></li> |
| 85 | + <li>There is at least one element <code>i</code> where <code>nums[i] == 0</code>.</li> |
| 86 | +</ul> |
| 87 | + |
| 88 | +<!-- description:end --> |
| 89 | + |
| 90 | +## Solutions |
| 91 | + |
| 92 | +<!-- solution:start --> |
| 93 | + |
| 94 | +### Solution 1 |
| 95 | + |
| 96 | +<!-- tabs:start --> |
| 97 | + |
| 98 | +#### Python3 |
| 99 | + |
| 100 | +```python |
| 101 | + |
| 102 | +``` |
| 103 | + |
| 104 | +#### Java |
| 105 | + |
| 106 | +```java |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +#### C++ |
| 111 | + |
| 112 | +```cpp |
| 113 | + |
| 114 | +``` |
| 115 | + |
| 116 | +#### Go |
| 117 | + |
| 118 | +```go |
| 119 | + |
| 120 | +``` |
| 121 | + |
| 122 | +<!-- tabs:end --> |
| 123 | + |
| 124 | +<!-- solution:end --> |
| 125 | + |
| 126 | +<!-- problem:end --> |
0 commit comments