forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_487.java
33 lines (31 loc) · 990 Bytes
/
_487.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.fishercoder.solutions;
public class _487 {
public static class Solution1 {
/**
* I implemented this on my own after a quick read from https://leetcode.com/problems/max-consecutive-ones-ii/solution/
*/
public static int findMaxConsecutiveOnes(int[] nums) {
int left = 0;
int right = 0;
int zeroes = 0;
int ans = 0;
while (right < nums.length) {
if (nums[right] == 0) {
zeroes++;
}
if (zeroes <= 1) {
ans = Math.max(ans, right - left + 1);
} else {
while (left < nums.length && zeroes > 1) {
if (nums[left] == 0) {
zeroes--;
}
left++;
}
}
right++;
}
return ans;
}
}
}