典型的なスライディングウィンドウの問題では、スライディングウィンドウの0の数がKを超えないようにスライディングウィンドウを維持します。
class Solution {
public:
int longestOnes(vector<int>& A, int K) {
int n = A.size(), res = 0, count =0;
for(int i=0,j=0;j<n;j++){
if(A[j]==0) count++;
while(i<=j&&count>K){
if(A[i]==0) count--;
i++;
}
res = max(res,j-i+1);
}
return res;
}
};