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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int longestSubarray(int[] nums) {
int zeroCount = 0;
int maxLength = 0;

int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] == 0) {
zeroCount++;
}

while (zeroCount > 1) {
if (nums[i] == 0) {
zeroCount--;
}
i++;
}

maxLength = Math.max(maxLength, j - i);
}

return maxLength;
}
}

References

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