File tree Expand file tree Collapse file tree 8 files changed +377
-53
lines changed
0400-0499/0487.Max Consecutive Ones II
1000-1099/1004.Max Consecutive Ones III Expand file tree Collapse file tree 8 files changed +377
-53
lines changed Original file line number Diff line number Diff line change 59
59
60
60
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 ` nums ` 的长度。
61
61
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
+
62
74
<!-- tabs:start -->
63
75
64
76
### ** Python3**
@@ -106,6 +118,22 @@ class Solution:
106
118
return ans
107
119
```
108
120
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
+
109
137
### ** Java**
110
138
111
139
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -163,6 +191,24 @@ class Solution {
163
191
}
164
192
```
165
193
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
+
166
212
### ** C++**
167
213
168
214
``` cpp
@@ -219,6 +265,25 @@ public:
219
265
};
220
266
```
221
267
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
+
222
287
### **Go**
223
288
224
289
```go
@@ -293,6 +358,25 @@ func max(a, b int) int {
293
358
}
294
359
```
295
360
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
+
296
380
### ** ...**
297
381
298
382
```
Original file line number Diff line number Diff line change @@ -87,6 +87,22 @@ class Solution:
87
87
return ans
88
88
```
89
89
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
+
90
106
### ** Java**
91
107
92
108
``` java
@@ -142,6 +158,24 @@ class Solution {
142
158
}
143
159
```
144
160
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
+
145
179
### ** C++**
146
180
147
181
``` cpp
@@ -198,6 +232,25 @@ public:
198
232
};
199
233
```
200
234
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
+
201
254
### **Go**
202
255
203
256
```go
@@ -272,6 +325,25 @@ func max(a, b int) int {
272
325
}
273
326
```
274
327
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
+
275
347
### ** ...**
276
348
277
349
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
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 ;
9
9
}
10
- while (cnt > 1 ) {
11
- if (nums[j++] == 0 ) {
12
- --cnt;
13
- }
10
+ if (k < 0 && nums[l++] == 0 ) {
11
+ ++k;
14
12
}
15
- ans = max (ans, i - j + 1 );
16
13
}
17
- return ans ;
14
+ return r - l ;
18
15
}
19
16
};
Original file line number Diff line number Diff line change 1
1
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 --
7
7
}
8
- for cnt > 1 {
9
- if nums [j ] == 0 {
10
- cnt --
8
+ if k < 0 {
9
+ if nums [l ] == 0 {
10
+ k ++
11
11
}
12
- j ++
12
+ l ++
13
13
}
14
- ans = max (ans , i - j + 1 )
15
14
}
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
24
16
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
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 ;
8
8
}
9
- while (cnt > 1 ) {
10
- if (nums [j ++] == 0 ) {
11
- --cnt ;
12
- }
9
+ if (k < 0 && nums [l ++] == 0 ) {
10
+ ++k ;
13
11
}
14
- ans = Math .max (ans , i - j + 1 );
15
12
}
16
- return ans ;
13
+ return r - l ;
17
14
}
18
15
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
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
You can’t perform that action at this time.
0 commit comments