Leetcode 1004 Max Consecutive Ones III Solution in java | Hindi Coding Community

0

 



Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.


Example 1:


Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2

Output: 6


class Solution {
public int longestOnes(int[] A, int K) {
int res = 0, count = 0, start = 0;
for (int i = 0; i < A.length; i++) {
if (A[i] == 0) count++;
if (count > K) {
res = Math.max(res, i-start);
while (A[start] == 1) start++;
start++;
count--;
System.out.println(res + " " + start + " " + i);
}
}
res = Math.max(res, A.length-start);
return res;
}
}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !