- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution1004.java
More file actions
Latest commit
34 lines (30 loc) · 926 Bytes
/
Copy pathSolution1004.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
publicclassSolution1004 {
publicintlongestOnes(int[] A, intK) {
intmaxLength = -1;
intlength = A.length;
intleft = 0;
intright = 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;
}
}
returnmaxLength;
}
publicstaticvoidmain(String[] args) {
Solution1004solution1004 = newSolution1004();
System.out.println(solution1004.longestOnes(newint[]{0, 0, 1, 1, 1, 0, 0}, 0));
}
}