-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxConsecutiveOnesIII.java
More file actions
36 lines (29 loc) · 926 Bytes
/
Copy pathMaxConsecutiveOnesIII.java
File metadata and controls
36 lines (29 loc) · 926 Bytes
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
34
35
36
package LeetcodePractice;
public class MaxConsecutiveOnesIII {
public int longestOnes(int[] nums, int k) {
int left = 0, right = 0;
int maxLen = 0;
int zeroCount = 0;
while (right < nums.length) {
if (nums[right] == 0) {
zeroCount++;
}
while (zeroCount > k) {
if (nums[left] == 0) {
zeroCount--;
}
left++;
}
maxLen = Math.max(maxLen, right - left + 1);
right++;
}
return maxLen;
}
public static void main(String[] args) {
MaxConsecutiveOnesIII sol = new MaxConsecutiveOnesIII();
System.out.println(sol.longestOnes(
new int[]{1,1,1,0,0,0,1,1,1,1,0}, 2));
System.out.println(sol.longestOnes(
new int[]{0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1}, 3));
}
}