-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtrionic-array-ii.cpp
More file actions
32 lines (31 loc) · 1.06 KB
/
trionic-array-ii.cpp
File metadata and controls
32 lines (31 loc) · 1.06 KB
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
// Time: O(n)
// Space: O(1)
// two pointers, greedy
class Solution {
public:
long long maxSumTrionic(vector<int>& nums) {
int64_t result = numeric_limits<int64_t>::min();
for (int64_t right = 1, left = 0, p = 0, q = 0, prefix = nums[0]; right < size(nums); ++right) {
prefix += nums[right];
if (nums[right - 1] > nums[right]) {
if (right - 2 >= 0 && nums[right - 2] < nums[right - 1]) {
p = right - 1;
while (left < q || (nums[left] < 0 && left + 1 < p)) {
prefix -= nums[left++];
}
}
} else if (nums[right - 1] < nums[right]) {
if (right - 2 >= 0 && nums[right - 2] > nums[right - 1]) {
q = right - 1;
}
if (left != p) {
result = max(result, prefix);
}
} else {
left = p = q = right;
prefix = nums[right];
}
}
return result;
}
};