[LeetCode] Longest Subarray of 1`s After Deleting One Element

1493. Longest Subarray of 1’s After Deleting One Element

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
class Solution {
public:
int longestSubarray(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;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/19/PS/LeetCode/longest-subarray-of-1s-after-deleting-one-element/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.