-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1493.cpp
More file actions
30 lines (29 loc) · 831 Bytes
/
1493.cpp
File metadata and controls
30 lines (29 loc) · 831 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
class Solution {
public:
int longestSubarray(vector<int>& nums) {
int n = nums.size();
vector<int> fromLeft(n, 0);
vector<int> fromRight(n, 0);
int current = 0;
for (int i = 0; i < n; ++i) {
if (nums[i] == 1) current += 1;
else current = 0;
fromLeft[i] = current;
}
current = 0;
for (int i = n - 1; i >= 0; --i) {
if (nums[i] == 1) current += 1;
else current = 0;
fromRight[i] = current;
}
int res = 0;
for (int i = 0; i < n; ++i) {
int left = 0;
if (i > 0) left = fromLeft[i - 1];
int right = 0;
if (i < n - 1) right = fromRight[i + 1];
res = max(res, left + right);
}
return res;
}
};