public class DayCode {
public static void main(String[] args) {
int[] A = new int[]{0, 1, 0};
int K = 1;
int ans = new DayCode().longestOnes(A, K);
System.out.println("ans is " + ans);
}
/**
* https://leetcode-cn.com/problems/max-consecutive-ones-iii/
* 时间复杂度O(n),空间复杂度O(1)
* @param A
* @param K
* @return
*/
public int longestOnes(int[] A, int K) {
int count = 0;
int left = 0;
int right = 0;
int ans = 0;
while (right < A.length) {
if (A[right++] == 0) {
count++;
}
while (count > K) {
if (A[left++] == 0) {
count--;
}
}
ans = Math.max(ans, right - left);
}
return ans;
}
}
评论