-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution1004.java
More file actions
34 lines (30 loc) · 926 Bytes
/
Solution1004.java
File metadata and controls
34 lines (30 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
public class Solution1004 {
public int longestOnes(int[] A, int K) {
int maxLength = -1;
int length = A.length;
int left = 0;
int right = 0;
while (left < length || right < length) {
while ((right < length) && (A[right] == 1 || K > 0)) {
if (A[right] == 1) {
++right;
} else {
++right;
--K;
}
}
maxLength = Math.max(maxLength, right - left);
if (left < length && A[left] == 1) {
++left;
} else {
++left;
++K;
}
}
return maxLength;
}
public static void main(String[] args) {
Solution1004 solution1004 = new Solution1004();
System.out.println(solution1004.longestOnes(new int[]{0, 0, 1, 1, 1, 0, 0}, 0));
}
}