Skip to content

Commit 2cb827f

Browse files
update 1004
1 parent 89a4460 commit 2cb827f

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_1004.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
public class _1004 {
44
public static class Solution1 {
5-
public int longestOnes(int[] A, int k) {
5+
/**
6+
* Two pointer technique, a.k.a sliding window.
7+
*/
8+
public int longestOnes(int[] nums, int k) {
69
int result = 0;
7-
int i = 0;
8-
for (int j = 0; j < A.length; j++) {
9-
if (A[j] == 0) {
10+
int left = 0;
11+
for (int right = 0; right < nums.length; right++) {
12+
if (nums[right] == 0) {
1013
k--;
1114
}
1215
while (k < 0) {
13-
if (A[i] == 0) {
16+
//in this case, we'll move the left pointer to the right
17+
if (nums[left] == 0) {
1418
k++;
1519
}
16-
i++;
20+
left++;
1721
}
18-
result = Math.max(result, j - i + 1);
22+
result = Math.max(result, right - left + 1);
1923
}
2024
return result;
2125
}

0 commit comments

Comments
 (0)