classSolution { public: intlongestSubarray(vector<int>& nums){ // the same with 1004. Max Consecutive Ones III int ret= 0; int l=0, r=0, n=nums.size(); int window =0; int del=1; while(r<n) { int cur = nums[r++]; if(cur == 0){ if(del>=0){ del--; } } else window++; while( del<0) { int prev = nums[l++]; if(prev==0) del++; else window--; } ret = max(ret, window - del); // - del , must delete one element. } return ret; } };