1004_MaxConsecutiveOnesIII - a920604a/leetcode GitHub Wiki


categories: leetcode comments: false tags:

  • Sliding Window title: 1004. Max Consecutive Ones III

solution

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int window=0, ret =0, l=0, r=0, n=nums.size();
        int f =k;
        while(r<n){
            int a = nums[r++];
            window+=a;
            if(a==0) f--;
            while(f<0){
                int b = nums[l++];
                if(b==0) f++;
                window-=b;
            }
            ret = max(ret, r-l);
        }
        return ret;   
    }
};

analysis

  • time complexity O(n)
  • space complexity O(1)
⚠️ **GitHub.com Fallback** ⚠️