LeetCode1004. Maximum number of consecutive 1s III

class Solution {
    
    
public:
    int longestOnes(vector<int>& A, int K) {
    
    
        int n = A.size();

        int left = 0;
        int right = 0;

        int max_cnt = 0;
           
        int map[2] = {
    
     0 };

        int res = 0;

        while (right < n)
        {
    
    
            map[A[right]]++;

            max_cnt = max(max_cnt, map[1]);

            if (right - left + 1 > max_cnt + K)
            {
    
    
                map[A[left]]--;
                left++;
            }
            else
            {
    
    
                res = max(res, right - left + 1);
            }
            right++;
        }

        return res;
    }
};

Guess you like

Origin blog.csdn.net/qq_32862515/article/details/109235645