1493_LongestSubarrayof1'sAfterDeletingOneElement - a920604a/leetcode GitHub Wiki


categories: leetcode comments: false tags:

  • Sliding Window title: 1493. Longest Subarray of 1's After Deleting One Element

solution

class Solution {
public:
    int longestSubarray(vector<int>& nums) {
        int f=1;
        int window=0, ret=0, l=0, r=0, n=nums.size();
        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-1);
        }
        return ret;
    }
};

analysis

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