Skip to content

Commit 09083f5

Browse files
authored
feat: add solutions to lc problems: No.3354~3356 (#3769)
1 parent 7bbe144 commit 09083f5

File tree

21 files changed

+969
-22
lines changed

21 files changed

+969
-22
lines changed

solution/3300-3399/3354.Make Array Elements Equal to Zero/README.md

+93-4
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,121 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3354.Ma
9393

9494
<!-- solution:start -->
9595

96-
### 方法一
96+
### 方法一:枚举 + 前缀和
97+
98+
假设我们初始向左移动,遇到了一个非零元素,那么我们就需要将这个元素减一,然后改变移动方向,继续移动。
99+
100+
因此,我们可以维护每个零值元素左侧的元素和 $l$,右侧元素的和 $s - l$。如果 $l = s - l$,即左侧元素和等于右侧元素和,那么我们可以选择当前零值元素,向左或向右移动,答案加 $2$;如果 $|l - (s - l)| = 1$,此时如果左侧元素和更大,那么我们可以选择当前零值元素,向左移动,答案加 $1$,如果右侧元素和更大,那么我们可以选择当前零值元素,向右移动,答案加 $1$。
101+
102+
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。
97103

98104
<!-- tabs:start -->
99105

100106
#### Python3
101107

102108
```python
103-
109+
class Solution:
110+
def countValidSelections(self, nums: List[int]) -> int:
111+
s = sum(nums)
112+
ans = l = 0
113+
for x in nums:
114+
if x:
115+
l += x
116+
elif l * 2 == s:
117+
ans += 2
118+
elif abs(l * 2 - s) == 1:
119+
ans += 1
120+
return ans
104121
```
105122

106123
#### Java
107124

108125
```java
109-
126+
class Solution {
127+
public int countValidSelections(int[] nums) {
128+
int s = Arrays.stream(nums).sum();
129+
int ans = 0, l = 0;
130+
for (int x : nums) {
131+
if (x != 0) {
132+
l += x;
133+
} else if (l * 2 == s) {
134+
ans += 2;
135+
} else if (Math.abs(l * 2 - s) <= 1) {
136+
++ans;
137+
}
138+
}
139+
return ans;
140+
}
141+
}
110142
```
111143

112144
#### C++
113145

114146
```cpp
115-
147+
class Solution {
148+
public:
149+
int countValidSelections(vector<int>& nums) {
150+
int s = accumulate(nums.begin(), nums.end(), 0);
151+
int ans = 0, l = 0;
152+
for (int x : nums) {
153+
if (x) {
154+
l += x;
155+
} else if (l * 2 == s) {
156+
ans += 2;
157+
} else if (abs(l * 2 - s) <= 1) {
158+
++ans;
159+
}
160+
}
161+
return ans;
162+
}
163+
};
116164
```
117165
118166
#### Go
119167
120168
```go
169+
func countValidSelections(nums []int) (ans int) {
170+
l, s := 0, 0
171+
for _, x := range nums {
172+
s += x
173+
}
174+
for _, x := range nums {
175+
if x != 0 {
176+
l += x
177+
} else if l*2 == s {
178+
ans += 2
179+
} else if abs(l*2-s) <= 1 {
180+
ans++
181+
}
182+
}
183+
return
184+
}
185+
186+
func abs(x int) int {
187+
if x < 0 {
188+
return -x
189+
}
190+
return x
191+
}
192+
```
121193

194+
#### TypeScript
195+
196+
```ts
197+
function countValidSelections(nums: number[]): number {
198+
const s = nums.reduce((acc, x) => acc + x, 0);
199+
let [ans, l] = [0, 0];
200+
for (const x of nums) {
201+
if (x) {
202+
l += x;
203+
} else if (l * 2 === s) {
204+
ans += 2;
205+
} else if (Math.abs(l * 2 - s) <= 1) {
206+
++ans;
207+
}
208+
}
209+
return ans;
210+
}
122211
```
123212

124213
<!-- tabs:end -->

solution/3300-3399/3354.Make Array Elements Equal to Zero/README_EN.md

+93-4
Original file line numberDiff line numberDiff line change
@@ -91,32 +91,121 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3354.Ma
9191

9292
<!-- solution:start -->
9393

94-
### Solution 1
94+
### Solution 1: Enumeration + Prefix Sum
95+
96+
Suppose we initially move to the left and encounter a non-zero element. In that case, we need to decrement this element by one, then change the direction of movement and continue moving.
97+
98+
Therefore, we can maintain the sum of elements to the left of each zero-value element as $l$, and the sum of elements to the right as $s - l$. If $l = s - l$, meaning the sum of elements on the left equals the sum of elements on the right, we can choose the current zero-value element and move either left or right, adding $2$ to the answer. If $|l - (s - l)| = 1$, and the sum of elements on the left is greater, we can choose the current zero-value element and move left, adding $1$ to the answer. If the sum of elements on the right is greater, we can choose the current zero-value element and move right, adding $1$ to the answer.
99+
100+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
95101

96102
<!-- tabs:start -->
97103

98104
#### Python3
99105

100106
```python
101-
107+
class Solution:
108+
def countValidSelections(self, nums: List[int]) -> int:
109+
s = sum(nums)
110+
ans = l = 0
111+
for x in nums:
112+
if x:
113+
l += x
114+
elif l * 2 == s:
115+
ans += 2
116+
elif abs(l * 2 - s) == 1:
117+
ans += 1
118+
return ans
102119
```
103120

104121
#### Java
105122

106123
```java
107-
124+
class Solution {
125+
public int countValidSelections(int[] nums) {
126+
int s = Arrays.stream(nums).sum();
127+
int ans = 0, l = 0;
128+
for (int x : nums) {
129+
if (x != 0) {
130+
l += x;
131+
} else if (l * 2 == s) {
132+
ans += 2;
133+
} else if (Math.abs(l * 2 - s) <= 1) {
134+
++ans;
135+
}
136+
}
137+
return ans;
138+
}
139+
}
108140
```
109141

110142
#### C++
111143

112144
```cpp
113-
145+
class Solution {
146+
public:
147+
int countValidSelections(vector<int>& nums) {
148+
int s = accumulate(nums.begin(), nums.end(), 0);
149+
int ans = 0, l = 0;
150+
for (int x : nums) {
151+
if (x) {
152+
l += x;
153+
} else if (l * 2 == s) {
154+
ans += 2;
155+
} else if (abs(l * 2 - s) <= 1) {
156+
++ans;
157+
}
158+
}
159+
return ans;
160+
}
161+
};
114162
```
115163
116164
#### Go
117165
118166
```go
167+
func countValidSelections(nums []int) (ans int) {
168+
l, s := 0, 0
169+
for _, x := range nums {
170+
s += x
171+
}
172+
for _, x := range nums {
173+
if x != 0 {
174+
l += x
175+
} else if l*2 == s {
176+
ans += 2
177+
} else if abs(l*2-s) <= 1 {
178+
ans++
179+
}
180+
}
181+
return
182+
}
183+
184+
func abs(x int) int {
185+
if x < 0 {
186+
return -x
187+
}
188+
return x
189+
}
190+
```
119191

192+
#### TypeScript
193+
194+
```ts
195+
function countValidSelections(nums: number[]): number {
196+
const s = nums.reduce((acc, x) => acc + x, 0);
197+
let [ans, l] = [0, 0];
198+
for (const x of nums) {
199+
if (x) {
200+
l += x;
201+
} else if (l * 2 === s) {
202+
ans += 2;
203+
} else if (Math.abs(l * 2 - s) <= 1) {
204+
++ans;
205+
}
206+
}
207+
return ans;
208+
}
120209
```
121210

122211
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int countValidSelections(vector<int>& nums) {
4+
int s = accumulate(nums.begin(), nums.end(), 0);
5+
int ans = 0, l = 0;
6+
for (int x : nums) {
7+
if (x) {
8+
l += x;
9+
} else if (l * 2 == s) {
10+
ans += 2;
11+
} else if (abs(l * 2 - s) <= 1) {
12+
++ans;
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func countValidSelections(nums []int) (ans int) {
2+
l, s := 0, 0
3+
for _, x := range nums {
4+
s += x
5+
}
6+
for _, x := range nums {
7+
if x != 0 {
8+
l += x
9+
} else if l*2 == s {
10+
ans += 2
11+
} else if abs(l*2-s) <= 1 {
12+
ans++
13+
}
14+
}
15+
return
16+
}
17+
18+
func abs(x int) int {
19+
if x < 0 {
20+
return -x
21+
}
22+
return x
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int countValidSelections(int[] nums) {
3+
int s = Arrays.stream(nums).sum();
4+
int ans = 0, l = 0;
5+
for (int x : nums) {
6+
if (x != 0) {
7+
l += x;
8+
} else if (l * 2 == s) {
9+
ans += 2;
10+
} else if (Math.abs(l * 2 - s) <= 1) {
11+
++ans;
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def countValidSelections(self, nums: List[int]) -> int:
3+
s = sum(nums)
4+
ans = l = 0
5+
for x in nums:
6+
if x:
7+
l += x
8+
elif l * 2 == s:
9+
ans += 2
10+
elif abs(l * 2 - s) == 1:
11+
ans += 1
12+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function countValidSelections(nums: number[]): number {
2+
const s = nums.reduce((acc, x) => acc + x, 0);
3+
let [ans, l] = [0, 0];
4+
for (const x of nums) {
5+
if (x) {
6+
l += x;
7+
} else if (l * 2 === s) {
8+
ans += 2;
9+
} else if (Math.abs(l * 2 - s) <= 1) {
10+
++ans;
11+
}
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
 (0)