リートコード1004。連続する1の最大数III(スライディングウィンドウ)

典型的なスライディングウィンドウの問題では、スライディングウィンドウの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;
    }
};

 

おすすめ

転載: blog.csdn.net/wwxy1995/article/details/113855800