Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1’s in the resulting array. Return 0 if there is no such subarray.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: intlongestSubarray(vector<int>& A){ int l = 0, r = 0, n = A.size(), z = 0, res = 0; while(r < n) { while(r < n and z <= 1) { z += A[r++] == 0; if(z <= 1) res = max(res, r - l - 1); } while(r < n and z > 1) { z -= A[l++] == 0; } } return res; } };