We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 89a4460 commit 2cb827fCopy full SHA for 2cb827f
src/main/java/com/fishercoder/solutions/_1004.java
@@ -2,20 +2,24 @@
2
3
public class _1004 {
4
public static class Solution1 {
5
- public int longestOnes(int[] A, int k) {
+ /**
6
+ * Two pointer technique, a.k.a sliding window.
7
+ */
8
+ public int longestOnes(int[] nums, int k) {
9
int result = 0;
- int i = 0;
- for (int j = 0; j < A.length; j++) {
- if (A[j] == 0) {
10
+ int left = 0;
11
+ for (int right = 0; right < nums.length; right++) {
12
+ if (nums[right] == 0) {
13
k--;
14
}
15
while (k < 0) {
- if (A[i] == 0) {
16
+ //in this case, we'll move the left pointer to the right
17
+ if (nums[left] == 0) {
18
k++;
19
- i++;
20
+ left++;
21
- result = Math.max(result, j - i + 1);
22
+ result = Math.max(result, right - left + 1);
23
24
return result;
25
0 commit comments