Skip to content

Commit 4689fbe

Browse files
committed
feat: add solutions to lc problems: No.0487,1004
* No.0487.Max Consecutive Ones II * No.1004.Max Consecutive Ones III
1 parent 5c0658f commit 4689fbe

File tree

8 files changed

+377
-53
lines changed

8 files changed

+377
-53
lines changed

solution/0400-0499/0487.Max Consecutive Ones II/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@
5959

6060
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 `nums` 的长度。
6161

62+
相似题目:[1004. 最大连续 1 的个数 III](/solution/1000-1099/1004.Max%20Consecutive%20Ones%20III/README.md)
63+
64+
以下是滑动窗口的优化版本。
65+
66+
维护一个单调变长的窗口。这种窗口经常出现在寻求“最大窗口”的问题中:因为求的是“最大”,所以我们没有必要缩短窗口,于是代码就少了缩短窗口的部分;从另一个角度讲,本题里的 K 是资源数,一旦透支,窗口就不能再增长了。
67+
68+
- l 是窗口左端点,负责移动起始位置
69+
- r 是窗口右端点,负责扩展窗口
70+
- k 是资源数,每次要替换 0,k 减 1,同时 r 向右移动
71+
- `r++` 每次都会执行,`l++` 只有资源 `k < 0` 时才触发,因此 `r - l` 的值只会单调递增(或保持不变)
72+
- 移动左端点时,如果当前元素是 0,说明可以释放一个资源,k 加 1
73+
6274
<!-- tabs:start -->
6375

6476
### **Python3**
@@ -106,6 +118,22 @@ class Solution:
106118
return ans
107119
```
108120

121+
```python
122+
class Solution:
123+
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
124+
l = r = 0
125+
k = 1
126+
while r < len(nums):
127+
if nums[r] == 0:
128+
k -= 1
129+
if k < 0:
130+
if nums[l] == 0:
131+
k += 1
132+
l += 1
133+
r += 1
134+
return r - l
135+
```
136+
109137
### **Java**
110138

111139
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -163,6 +191,24 @@ class Solution {
163191
}
164192
```
165193

194+
```java
195+
class Solution {
196+
public int findMaxConsecutiveOnes(int[] nums) {
197+
int l = 0, r = 0;
198+
int k = 1;
199+
while (r < nums.length) {
200+
if (nums[r++] == 0) {
201+
--k;
202+
}
203+
if (k < 0 && nums[l++] == 0) {
204+
++k;
205+
}
206+
}
207+
return r - l;
208+
}
209+
}
210+
```
211+
166212
### **C++**
167213

168214
```cpp
@@ -219,6 +265,25 @@ public:
219265
};
220266
```
221267

268+
```cpp
269+
class Solution {
270+
public:
271+
int findMaxConsecutiveOnes(vector<int>& nums) {
272+
int l = 0, r = 0;
273+
int k = 1;
274+
while (r < nums.size()) {
275+
if (nums[r++] == 0) {
276+
--k;
277+
}
278+
if (k < 0 && nums[l++] == 0) {
279+
++k;
280+
}
281+
}
282+
return r - l;
283+
}
284+
};
285+
```
286+
222287
### **Go**
223288
224289
```go
@@ -293,6 +358,25 @@ func max(a, b int) int {
293358
}
294359
```
295360

361+
```go
362+
func findMaxConsecutiveOnes(nums []int) int {
363+
l, r := 0, 0
364+
k := 1
365+
for ; r < len(nums); r++ {
366+
if nums[r] == 0 {
367+
k--
368+
}
369+
if k < 0 {
370+
if nums[l] == 0 {
371+
k++
372+
}
373+
l++
374+
}
375+
}
376+
return r - l
377+
}
378+
```
379+
296380
### **...**
297381

298382
```

solution/0400-0499/0487.Max Consecutive Ones II/README_EN.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ class Solution:
8787
return ans
8888
```
8989

90+
```python
91+
class Solution:
92+
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
93+
l = r = 0
94+
k = 1
95+
while r < len(nums):
96+
if nums[r] == 0:
97+
k -= 1
98+
if k < 0:
99+
if nums[l] == 0:
100+
k += 1
101+
l += 1
102+
r += 1
103+
return r - l
104+
```
105+
90106
### **Java**
91107

92108
```java
@@ -142,6 +158,24 @@ class Solution {
142158
}
143159
```
144160

161+
```java
162+
class Solution {
163+
public int findMaxConsecutiveOnes(int[] nums) {
164+
int l = 0, r = 0;
165+
int k = 1;
166+
while (r < nums.length) {
167+
if (nums[r++] == 0) {
168+
--k;
169+
}
170+
if (k < 0 && nums[l++] == 0) {
171+
++k;
172+
}
173+
}
174+
return r - l;
175+
}
176+
}
177+
```
178+
145179
### **C++**
146180

147181
```cpp
@@ -198,6 +232,25 @@ public:
198232
};
199233
```
200234

235+
```cpp
236+
class Solution {
237+
public:
238+
int findMaxConsecutiveOnes(vector<int>& nums) {
239+
int l = 0, r = 0;
240+
int k = 1;
241+
while (r < nums.size()) {
242+
if (nums[r++] == 0) {
243+
--k;
244+
}
245+
if (k < 0 && nums[l++] == 0) {
246+
++k;
247+
}
248+
}
249+
return r - l;
250+
}
251+
};
252+
```
253+
201254
### **Go**
202255
203256
```go
@@ -272,6 +325,25 @@ func max(a, b int) int {
272325
}
273326
```
274327

328+
```go
329+
func findMaxConsecutiveOnes(nums []int) int {
330+
l, r := 0, 0
331+
k := 1
332+
for ; r < len(nums); r++ {
333+
if nums[r] == 0 {
334+
k--
335+
}
336+
if k < 0 {
337+
if nums[l] == 0 {
338+
k++
339+
}
340+
l++
341+
}
342+
}
343+
return r - l
344+
}
345+
```
346+
275347
### **...**
276348

277349
```
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
class Solution {
22
public:
33
int findMaxConsecutiveOnes(vector<int>& nums) {
4-
int ans = 1;
5-
int cnt = 0, j = 0;
6-
for (int i = 0; i < nums.size(); ++i) {
7-
if (nums[i] == 0) {
8-
++cnt;
4+
int l = 0, r = 0;
5+
int k = 1;
6+
while (r < nums.size()) {
7+
if (nums[r++] == 0) {
8+
--k;
99
}
10-
while (cnt > 1) {
11-
if (nums[j++] == 0) {
12-
--cnt;
13-
}
10+
if (k < 0 && nums[l++] == 0) {
11+
++k;
1412
}
15-
ans = max(ans, i - j + 1);
1613
}
17-
return ans;
14+
return r - l;
1815
}
1916
};
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
func findMaxConsecutiveOnes(nums []int) int {
2-
ans := 1
3-
j, cnt := 0, 0
4-
for i, v := range nums {
5-
if v == 0 {
6-
cnt++
2+
l, r := 0, 0
3+
k := 1
4+
for ; r < len(nums); r++ {
5+
if nums[r] == 0 {
6+
k--
77
}
8-
for cnt > 1 {
9-
if nums[j] == 0 {
10-
cnt--
8+
if k < 0 {
9+
if nums[l] == 0 {
10+
k++
1111
}
12-
j++
12+
l++
1313
}
14-
ans = max(ans, i-j+1)
1514
}
16-
return ans
17-
}
18-
19-
func max(a, b int) int {
20-
if a > b {
21-
return a
22-
}
23-
return b
15+
return r - l
2416
}
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
class Solution {
22
public int findMaxConsecutiveOnes(int[] nums) {
3-
int j = 0, cnt = 0;
4-
int ans = 1;
5-
for (int i = 0; i < nums.length; ++i) {
6-
if (nums[i] == 0) {
7-
++cnt;
3+
int l = 0, r = 0;
4+
int k = 1;
5+
while (r < nums.length) {
6+
if (nums[r++] == 0) {
7+
--k;
88
}
9-
while (cnt > 1) {
10-
if (nums[j++] == 0) {
11-
--cnt;
12-
}
9+
if (k < 0 && nums[l++] == 0) {
10+
++k;
1311
}
14-
ans = Math.max(ans, i - j + 1);
1512
}
16-
return ans;
13+
return r - l;
1714
}
1815
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution:
22
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
3-
ans = 1
4-
cnt = j = 0
5-
for i, v in enumerate(nums):
6-
if v == 0:
7-
cnt += 1
8-
while cnt > 1:
9-
if nums[j] == 0:
10-
cnt -= 1
11-
j += 1
12-
ans = max(ans, i - j + 1)
13-
return ans
3+
l = r = 0
4+
k = 1
5+
while r < len(nums):
6+
if nums[r] == 0:
7+
k -= 1
8+
if k < 0:
9+
if nums[l] == 0:
10+
k += 1
11+
l += 1
12+
r += 1
13+
return r - l

0 commit comments

Comments
 (0)