@@ -60,152 +60,15 @@ The max number of consecutive ones is 4.
6060
6161<!-- solution:start -->
6262
63- ### Solution 1
63+ ### Solution 1: Sliding Window
6464
65- <!-- tabs:start -->
66-
67- #### Python3
68-
69- ``` python
70- class Solution :
71- def findMaxConsecutiveOnes (self , nums : List[int ]) -> int :
72- ans = nums.count(1 )
73- n = len (nums)
74- left = [0 ] * n
75- right = [0 ] * n
76- for i, v in enumerate (nums):
77- if v:
78- left[i] = 1 if i == 0 else left[i - 1 ] + 1
79- for i in range (n - 1 , - 1 , - 1 ):
80- v = nums[i]
81- if v:
82- right[i] = 1 if i == n - 1 else right[i + 1 ] + 1
83- ans = 0
84- for i, v in enumerate (nums):
85- t = 0
86- if i:
87- t += left[i - 1 ]
88- if i < n - 1 :
89- t += right[i + 1 ]
90- ans = max (ans, t + 1 )
91- return ans
92- ```
93-
94- #### Java
95-
96- ``` java
97- class Solution {
98- public int findMaxConsecutiveOnes (int [] nums ) {
99- int n = nums. length;
100- int [] left = new int [n];
101- int [] right = new int [n];
102- for (int i = 0 ; i < n; ++ i) {
103- if (nums[i] == 1 ) {
104- left[i] = i == 0 ? 1 : left[i - 1 ] + 1 ;
105- }
106- }
107- for (int i = n - 1 ; i >= 0 ; -- i) {
108- if (nums[i] == 1 ) {
109- right[i] = i == n - 1 ? 1 : right[i + 1 ] + 1 ;
110- }
111- }
112- int ans = 0 ;
113- for (int i = 0 ; i < n; ++ i) {
114- int t = 0 ;
115- if (i > 0 ) {
116- t += left[i - 1 ];
117- }
118- if (i < n - 1 ) {
119- t += right[i + 1 ];
120- }
121- ans = Math . max(ans, t + 1 );
122- }
123- return ans;
124- }
125- }
126- ```
127-
128- #### C++
129-
130- ``` cpp
131- class Solution {
132- public:
133- int findMaxConsecutiveOnes(vector<int >& nums) {
134- int n = nums.size();
135- vector<int > left(n), right(n);
136- for (int i = 0; i < n; ++i) {
137- if (nums[ i] ) {
138- left[ i] = i == 0 ? 1 : left[ i - 1] + 1;
139- }
140- }
141- for (int i = n - 1; ~ i; --i) {
142- if (nums[ i] ) {
143- right[ i] = i == n - 1 ? 1 : right[ i + 1] + 1;
144- }
145- }
146- int ans = 0;
147- for (int i = 0; i < n; ++i) {
148- int t = 0;
149- if (i) {
150- t += left[ i - 1] ;
151- }
152- if (i < n - 1) {
153- t += right[ i + 1] ;
154- }
155- ans = max(ans, t + 1);
156- }
157- return ans;
158- }
159- };
160- ```
161-
162- #### Go
163-
164- ```go
165- func findMaxConsecutiveOnes(nums []int) int {
166- n := len(nums)
167- left := make([]int, n)
168- right := make([]int, n)
169- for i, v := range nums {
170- if v == 1 {
171- if i == 0 {
172- left[i] = 1
173- } else {
174- left[i] = left[i-1] + 1
175- }
176- }
177- }
178- for i := n - 1; i >= 0; i-- {
179- if nums[i] == 1 {
180- if i == n-1 {
181- right[i] = 1
182- } else {
183- right[i] = right[i+1] + 1
184- }
185- }
186- }
187- ans := 0
188- for i := range nums {
189- t := 0
190- if i > 0 {
191- t += left[i-1]
192- }
193- if i < n-1 {
194- t += right[i+1]
195- }
196- ans = max(ans, t+1)
197- }
198- return ans
199- }
200- ```
65+ We can iterate through the array, using a variable $\textit{cnt}$ to record the current number of 0s in the window. When $\textit{cnt} > 1$, we move the left boundary of the window to the right by one position.
20166
202- <!-- tabs : end -->
67+ After the iteration ends, the length of the window is the maximum number of consecutive 1s.
20368
204- <!-- solution : end -->
69+ Note that in the process above, we do not need to loop to move the left boundary of the window to the right. Instead, we directly move the left boundary to the right by one position. This is because the problem asks for the maximum number of consecutive 1s, so the length of the window will only increase, not decrease. Therefore, we do not need to loop to move the left boundary to the right.
20570
206- <!-- solution: start -->
207-
208- ### Solution 2
71+ The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
20972
21073<!-- tabs:start -->
21174
@@ -214,38 +77,28 @@ func findMaxConsecutiveOnes(nums []int) int {
21477``` python
21578class Solution :
21679 def findMaxConsecutiveOnes (self , nums : List[int ]) -> int :
217- ans = 1
218- cnt = j = 0
219- for i, v in enumerate (nums):
220- if v == 0 :
221- cnt += 1
222- while cnt > 1 :
223- if nums[j] == 0 :
224- cnt -= 1
225- j += 1
226- ans = max (ans, i - j + 1 )
227- return ans
80+ l = cnt = 0
81+ for x in nums:
82+ cnt += x ^ 1
83+ if cnt > 1 :
84+ cnt -= nums[l] ^ 1
85+ l += 1
86+ return len (nums) - l
22887```
22988
23089#### Java
23190
23291``` java
23392class Solution {
23493 public int findMaxConsecutiveOnes (int [] nums ) {
235- int j = 0 , cnt = 0 ;
236- int ans = 1 ;
237- for ( int i = 0 ; i < nums . length; ++ i) {
238- if (nums[i] == 0 ) {
239- ++ cnt ;
94+ int l = 0 , cnt = 0 ;
95+ for ( int x : nums) {
96+ cnt += x ^ 1 ;
97+ if (cnt > 1 ) {
98+ cnt -= nums[l ++ ] ^ 1 ;
24099 }
241- while (cnt > 1 ) {
242- if (nums[j++ ] == 0 ) {
243- -- cnt;
244- }
245- }
246- ans = Math . max(ans, i - j + 1 );
247100 }
248- return ans ;
101+ return nums . length - l ;
249102 }
250103}
251104```
@@ -256,20 +109,14 @@ class Solution {
256109class Solution {
257110public:
258111 int findMaxConsecutiveOnes(vector<int >& nums) {
259- int ans = 1;
260- int cnt = 0, j = 0;
261- for (int i = 0; i < nums.size(); ++i) {
262- if (nums[ i] == 0) {
263- ++cnt;
264- }
265- while (cnt > 1) {
266- if (nums[ j++] == 0) {
267- --cnt;
268- }
112+ int l = 0, cnt = 0;
113+ for (int x : nums) {
114+ cnt += x ^ 1;
115+ if (cnt > 1) {
116+ cnt -= nums[ l++] ^ 1;
269117 }
270- ans = max(ans, i - j + 1);
271118 }
272- return ans ;
119+ return nums.size() - l ;
273120 }
274121};
275122```
@@ -278,114 +125,52 @@ public:
278125
279126```go
280127func findMaxConsecutiveOnes(nums []int) int {
281- ans := 1
282- j, cnt := 0, 0
283- for i, v := range nums {
284- if v == 0 {
285- cnt++
286- }
287- for cnt > 1 {
288- if nums[j] == 0 {
289- cnt--
290- }
291- j++
128+ l, cnt := 0, 0
129+ for _, x := range nums {
130+ cnt += x ^ 1
131+ if cnt > 1 {
132+ cnt -= nums[l] ^ 1
133+ l++
292134 }
293- ans = max(ans, i-j+1)
294135 }
295- return ans
136+ return len(nums) - l
296137}
297138```
298139
299- <!-- tabs: end -->
300-
301- <!-- solution: end -->
302-
303- <!-- solution: start -->
304-
305- ### Solution 3
306-
307- <!-- tabs: start -->
308-
309- #### Python3
310-
311- ``` python
312- class Solution :
313- def findMaxConsecutiveOnes (self , nums : List[int ]) -> int :
314- l = r = 0
315- k = 1
316- while r < len (nums):
317- if nums[r] == 0 :
318- k -= 1
319- if k < 0 :
320- if nums[l] == 0 :
321- k += 1
322- l += 1
323- r += 1
324- return r - l
325- ```
140+ #### TypeScript
326141
327- #### Java
328-
329- ``` java
330- class Solution {
331- public int findMaxConsecutiveOnes (int [] nums ) {
332- int l = 0 , r = 0 ;
333- int k = 1 ;
334- while (r < nums. length) {
335- if (nums[r++ ] == 0 ) {
336- -- k;
337- }
338- if (k < 0 && nums[l++ ] == 0 ) {
339- ++ k;
340- }
142+ ``` ts
143+ function findMaxConsecutiveOnes(nums : number []): number {
144+ let [l, cnt] = [0 , 0 ];
145+ for (const x of nums ) {
146+ cnt += x ^ 1 ;
147+ if (cnt > 1 ) {
148+ cnt -= nums [l ++ ] ^ 1 ;
341149 }
342- return r - l;
343150 }
151+ return nums .length - l ;
344152}
345153```
346154
347- #### C++
348-
349- ``` cpp
350- class Solution {
351- public:
352- int findMaxConsecutiveOnes(vector<int >& nums) {
353- int l = 0, r = 0;
354- int k = 1;
355- while (r < nums.size()) {
356- if (nums[ r++] == 0) {
357- --k;
358- }
359- if (k < 0 && nums[ l++] == 0) {
360- ++k;
361- }
155+ #### JavaScript
156+
157+ ``` js
158+ /**
159+ * @param {number[]} nums
160+ * @return {number}
161+ */
162+ var findMaxConsecutiveOnes = function (nums ) {
163+ let [l, cnt] = [0 , 0 ];
164+ for (const x of nums) {
165+ cnt += x ^ 1 ;
166+ if (cnt > 1 ) {
167+ cnt -= nums[l++ ] ^ 1 ;
362168 }
363- return r - l;
364169 }
170+ return nums .length - l;
365171};
366172```
367173
368- #### Go
369-
370- ```go
371- func findMaxConsecutiveOnes(nums []int) int {
372- l, r := 0, 0
373- k := 1
374- for ; r < len(nums); r++ {
375- if nums[r] == 0 {
376- k--
377- }
378- if k < 0 {
379- if nums[l] == 0 {
380- k++
381- }
382- l++
383- }
384- }
385- return r - l
386- }
387- ```
388-
389174<!-- tabs: end -->
390175
391176<!-- solution: end -->
0 commit comments