-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1493.cpp
More file actions
38 lines (31 loc) · 693 Bytes
/
1493.cpp
File metadata and controls
38 lines (31 loc) · 693 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<vector>
#include<unordered_map>
int longestSubarray(std::vector<int>& nums) {
if (nums.size() == 1) {
return 0;
}
int max = 0;
int zr_count = 0;
int left = 0;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == 0) {
++zr_count;
}
while (zr_count > 1) {
if (nums[left] == 0) {
--zr_count;
}
++left;
}
if (max < i - left) {
max = i - left;
}
}
return max;
}
int main () {
std::vector<int> nums = { 0,1,1,1,0,1,1,0,1 };
std::cout << longestSubarray(nums) << "\n";
std::cin.get();
}