1004. Max Consecutive Ones III
1004. Max Consecutive Ones III
题目描述: Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.
int longestOnes(vector<int>& A, int K) {
int res=0;
int f=0;
int start=0;
for(int i=0;i<A.size();++i) {
f+=1-A[i];
// 翻转次数多于K,则向前滑动
while(f>K) if (A[start++]==0) --f;
// 记录最大值
res=max(res, i-start+1);
}
return res;
}#滑动窗口