Longest Subarray of 1's After Deleting One Element LeetCode solution in C++

1
class Solution {
2
public:
3
int longestSubarray(vector<int> &nums) {
4
int x = 0, y = 0, cnt = 0;
5
for (int i = 0; i < nums.size(); i++) {
6
if (nums[i] == 1) {
7
x++;
8
} else if (nums[i] == 0) {
9
cnt = max(cnt, x + y);
10
y = x;
11
x = 0;
12
}
13
if (i == nums.size() - 1) {
14
cnt = max(cnt, x + y);
15
}
16
}
17
if (cnt == nums.size()) return cnt - 1;
18
return cnt;
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0