Skip to content

Commit 4689fbe

Browse files
committedSep 14, 2022
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

+84
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

+72
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
```
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
};
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
}
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
}
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

‎solution/1000-1099/1004.Max Consecutive Ones III/README.md

+98-2
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,48 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43-
思路同 [2024. 考试的最大困扰度](/solution/2000-2099/2024.Maximize%20the%20Confusion%20of%20an%20Exam/README.md)
43+
**方法一:滑动窗口**
4444

45-
维护一个单调变长的窗口。这种窗口经常出现在寻求“最大窗口”的问题中:因为要求的是”最大“,所以我们没有必要缩短窗口,于是代码就少了缩短窗口的部分;从另一个角度讲,本题里的 K 是资源数,一旦透支,窗口就不能再增长了。
45+
定义一个滑动窗口,窗口内的 $0$ 的个数不超过 $k$,窗口的右边界不断向右移动,当窗口内的 $0$ 的个数超过 $k$ 时,窗口的左边界向右移动,直到窗口内的 $0$ 的个数不超过 $k$ 为止。
46+
47+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
48+
49+
相似题目:[487. 最大连续 1 的个数 II](/solution/0400-0499/0487.Max%20Consecutive%20Ones%20II/README.md)
50+
51+
以下是滑动窗口的优化版本。
52+
53+
维护一个单调变长的窗口。这种窗口经常出现在寻求“最大窗口”的问题中:因为求的是“最大”,所以我们没有必要缩短窗口,于是代码就少了缩短窗口的部分;从另一个角度讲,本题里的 K 是资源数,一旦透支,窗口就不能再增长了。
4654

4755
- l 是窗口左端点,负责移动起始位置
4856
- r 是窗口右端点,负责扩展窗口
4957
- k 是资源数,每次要替换 0,k 减 1,同时 r 向右移动
5058
- `r++` 每次都会执行,`l++` 只有资源 `k < 0` 时才触发,因此 `r - l` 的值只会单调递增(或保持不变)
5159
- 移动左端点时,如果当前元素是 0,说明可以释放一个资源,k 加 1
5260

61+
相似题目: [2024. 考试的最大困扰度](/solution/2000-2099/2024.Maximize%20the%20Confusion%20of%20an%20Exam/README.md)
62+
5363
<!-- tabs:start -->
5464

5565
### **Python3**
5666

5767
<!-- 这里可写当前语言的特殊实现逻辑 -->
5868

69+
```python
70+
class Solution:
71+
def longestOnes(self, nums: List[int], k: int) -> int:
72+
ans = 0
73+
cnt = j = 0
74+
for i, v in enumerate(nums):
75+
if v == 0:
76+
cnt += 1
77+
while cnt > k:
78+
if nums[j] == 0:
79+
cnt -= 1
80+
j += 1
81+
ans = max(ans, i - j + 1)
82+
return ans
83+
```
84+
5985
```python
6086
class Solution:
6187
def longestOnes(self, nums: List[int], k: int) -> int:
@@ -75,6 +101,27 @@ class Solution:
75101

76102
<!-- 这里可写当前语言的特殊实现逻辑 -->
77103

104+
```java
105+
class Solution {
106+
public int longestOnes(int[] nums, int k) {
107+
int j = 0, cnt = 0;
108+
int ans = 0;
109+
for (int i = 0; i < nums.length; ++i) {
110+
if (nums[i] == 0) {
111+
++cnt;
112+
}
113+
while (cnt > k) {
114+
if (nums[j++] == 0) {
115+
--cnt;
116+
}
117+
}
118+
ans = Math.max(ans, i - j + 1);
119+
}
120+
return ans;
121+
}
122+
}
123+
```
124+
78125
```java
79126
class Solution {
80127
public int longestOnes(int[] nums, int k) {
@@ -94,6 +141,28 @@ class Solution {
94141

95142
### **C++**
96143

144+
```cpp
145+
class Solution {
146+
public:
147+
int longestOnes(vector<int>& nums, int k) {
148+
int ans = 0;
149+
int cnt = 0, j = 0;
150+
for (int i = 0; i < nums.size(); ++i) {
151+
if (nums[i] == 0) {
152+
++cnt;
153+
}
154+
while (cnt > k) {
155+
if (nums[j++] == 0) {
156+
--cnt;
157+
}
158+
}
159+
ans = max(ans, i - j + 1);
160+
}
161+
return ans;
162+
}
163+
};
164+
```
165+
97166
```cpp
98167
class Solution {
99168
public:
@@ -110,6 +179,33 @@ public:
110179

111180
### **Go**
112181

182+
```go
183+
func longestOnes(nums []int, k int) int {
184+
ans := 0
185+
j, cnt := 0, 0
186+
for i, v := range nums {
187+
if v == 0 {
188+
cnt++
189+
}
190+
for cnt > k {
191+
if nums[j] == 0 {
192+
cnt--
193+
}
194+
j++
195+
}
196+
ans = max(ans, i-j+1)
197+
}
198+
return ans
199+
}
200+
201+
func max(a, b int) int {
202+
if a > b {
203+
return a
204+
}
205+
return b
206+
}
207+
```
208+
113209
```go
114210
func longestOnes(nums []int, k int) int {
115211
l, r := -1, -1

‎solution/1000-1099/1004.Max Consecutive Ones III/README_EN.md

+86
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
3939

4040
### **Python3**
4141

42+
```python
43+
class Solution:
44+
def longestOnes(self, nums: List[int], k: int) -> int:
45+
ans = 0
46+
cnt = j = 0
47+
for i, v in enumerate(nums):
48+
if v == 0:
49+
cnt += 1
50+
while cnt > k:
51+
if nums[j] == 0:
52+
cnt -= 1
53+
j += 1
54+
ans = max(ans, i - j + 1)
55+
return ans
56+
```
57+
4258
```python
4359
class Solution:
4460
def longestOnes(self, nums: List[int], k: int) -> int:
@@ -56,6 +72,27 @@ class Solution:
5672

5773
### **Java**
5874

75+
```java
76+
class Solution {
77+
public int longestOnes(int[] nums, int k) {
78+
int j = 0, cnt = 0;
79+
int ans = 0;
80+
for (int i = 0; i < nums.length; ++i) {
81+
if (nums[i] == 0) {
82+
++cnt;
83+
}
84+
while (cnt > k) {
85+
if (nums[j++] == 0) {
86+
--cnt;
87+
}
88+
}
89+
ans = Math.max(ans, i - j + 1);
90+
}
91+
return ans;
92+
}
93+
}
94+
```
95+
5996
```java
6097
class Solution {
6198
public int longestOnes(int[] nums, int k) {
@@ -75,6 +112,28 @@ class Solution {
75112

76113
### **C++**
77114

115+
```cpp
116+
class Solution {
117+
public:
118+
int longestOnes(vector<int>& nums, int k) {
119+
int ans = 0;
120+
int cnt = 0, j = 0;
121+
for (int i = 0; i < nums.size(); ++i) {
122+
if (nums[i] == 0) {
123+
++cnt;
124+
}
125+
while (cnt > k) {
126+
if (nums[j++] == 0) {
127+
--cnt;
128+
}
129+
}
130+
ans = max(ans, i - j + 1);
131+
}
132+
return ans;
133+
}
134+
};
135+
```
136+
78137
```cpp
79138
class Solution {
80139
public:
@@ -91,6 +150,33 @@ public:
91150

92151
### **Go**
93152

153+
```go
154+
func longestOnes(nums []int, k int) int {
155+
ans := 0
156+
j, cnt := 0, 0
157+
for i, v := range nums {
158+
if v == 0 {
159+
cnt++
160+
}
161+
for cnt > k {
162+
if nums[j] == 0 {
163+
cnt--
164+
}
165+
j++
166+
}
167+
ans = max(ans, i-j+1)
168+
}
169+
return ans
170+
}
171+
172+
func max(a, b int) int {
173+
if a > b {
174+
return a
175+
}
176+
return b
177+
}
178+
```
179+
94180
```go
95181
func longestOnes(nums []int, k int) int {
96182
l, r := -1, -1

0 commit comments

Comments
 (0)
Please sign in to comment.