Skip to content

Commit de468a3

Browse files
committed
2 parents c296b98 + e387b03 commit de468a3

4 files changed

Lines changed: 164 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: 3354. Make Array Elements Equal to Zero
3+
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.
4+
keywords:
5+
[
6+
LeetCode,
7+
3354. Make Array Elements Equal to Zero,
8+
Easy,
9+
Array,
10+
Simulation,
11+
Prefix Sum,
12+
]
13+
tags: [LeetCode, Easy, Array, Simulation, Prefix Sum]
14+
---
15+
16+
# [{frontMatter.title}](https://leetcode.com/problems/make-array-elements-equal-to-zero/)
17+
18+
import Tabs from "@theme/Tabs";
19+
import TabItem from "@theme/TabItem";
20+
import CodeBlock from "@theme/CodeBlock";
21+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
22+
import Solution from "!!raw-loader!./solution.js";
23+
import Description from "./_Description.md";
24+
import Examples from "./_Examples.md";
25+
26+
<DifficultyBadge difficulty="Easy" />
27+
28+
<Tabs>
29+
<TabItem value="description" label="題目描述" default>
30+
<details open>
31+
<summary>Description</summary>
32+
<Description />
33+
<Examples />
34+
</details>
35+
</TabItem>
36+
37+
<TabItem value="solution" label="解答">
38+
## Solution
39+
<CodeBlock language="js">{Solution}</CodeBlock>
40+
</TabItem>
41+
</Tabs>
42+
43+
### 解題思路
44+
45+
不懂題目在講啥所以先點開 Discussion 看一下
46+
發現有人貼了這張圖
47+
![](https://assets.leetcode.com/users/images/918b572a-9773-4747-8c71-9e9f6945372c_1761508886.660334.webp)
48+
瞬間變得超好懂!
49+
50+
再回來看題目
51+
題目需要找到 `curr`,並讓 `nums[curr] == 0`
52+
最後回傳符合需求的 `curr` 數量
53+
54+
所以在程式裡面可以先定義 `result` 來記錄符合需求的 `curr` 數量
55+
再使用 for 迴圈來遍歷 `nums`,當不是 `0` 的時候就跳過這次迭代
56+
57+
```js
58+
var countValidSelections = function (nums) {
59+
let result = 0;
60+
61+
for (let i = 0; i < nums.length; i++) {
62+
if (nums[i] !== 0) continue;
63+
// TODO
64+
}
65+
66+
return result;
67+
};
68+
```
69+
70+
根據那張圖可以想成要找的其實就是以下三種狀況:
71+
72+
1. 左右兩邊總和相同
73+
- 代表不管起始方向是左或右都可以,方塊會被剛好消除光,所以 `result += 2`
74+
2. 左邊總和比右邊總和大 `1`
75+
- 代表起始方向是左時,方塊會被剛好消除光,但起始方向是右則不行,所以 `result += 1`
76+
3. 左邊總和比右邊總和小 `1`
77+
- 代表起始方向是右時,方塊會被剛好消除光,但起始方向是左則不行,所以 `result += 1`
78+
79+
再轉換成程式碼,透過 `sumLeft``sumRight` 算出左右總和,並篩選出三種狀況與對應的結果
80+
81+
```js
82+
const sumLeft = nums.slice(0, i).reduce((acc, cur) => acc + cur, 0);
83+
const sumRight = nums.slice(i).reduce((acc, cur) => acc + cur, 0);
84+
85+
if (sumLeft === sumRight) result += 2;
86+
if (sumLeft === sumRight - 1) result += 1;
87+
if (sumLeft === sumRight + 1) result += 1;
88+
```
89+
90+
### 心得
91+
92+
做圖人...我的超人...
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
You are given an integer array nums.
2+
3+
Start by selecting a starting position curr such that nums[curr] == 0, and choose a movement direction of either left or right.
4+
5+
After that, you repeat the following process:
6+
7+
If curr is out of the range [0, n - 1], this process ends.
8+
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.
9+
Else if nums[curr] > 0:
10+
Decrement nums[curr] by 1.
11+
Reverse your movement direction (left becomes right and vice versa).
12+
Take a step in your new direction.
13+
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.
14+
15+
Return the number of possible valid selections.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#### Example 1:
2+
3+
> **Input:** nums = [1,0,2,0,3]
4+
> **Output:** 2
5+
> **Explanation:**
6+
>
7+
> The only possible valid selections are the following:
8+
9+
Choose `curr = 3`, and a movement direction to the left.
10+
11+
```
12+
[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] ->
13+
[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] ->
14+
[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] ->
15+
[0,0,0,0,1] -> [0,0,0,0,0].
16+
```
17+
18+
Choose curr = 3, and a movement direction to the right.
19+
20+
```
21+
[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] ->
22+
[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] ->
23+
[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].
24+
```
25+
26+
#### Example 2:
27+
28+
> **Input:** nums = [2,3,4,0,4,1,0]
29+
> **Output:** 0
30+
> **Explanation:**
31+
> There are no possible valid selections.
32+
33+
#### Constraints:
34+
35+
- `1 <= nums.length <= 100`
36+
- `0 <= nums[i] <= 100`
37+
- There is at least one element `i` where `nums[i] == 0`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var countValidSelections = function (nums) {
6+
let result = 0;
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
if (nums[i] !== 0) continue;
10+
11+
const sumLeft = nums.slice(0, i).reduce((acc, cur) => acc + cur, 0);
12+
const sumRight = nums.slice(i).reduce((acc, cur) => acc + cur, 0);
13+
14+
if (sumLeft === sumRight) result += 2;
15+
if (sumLeft === sumRight - 1) result += 1;
16+
if (sumLeft === sumRight + 1) result += 1;
17+
}
18+
19+
return result;
20+
};

0 commit comments

Comments
 (0)