Skip to content

Commit 93f2601

Browse files
authored
Create 2036.Maximum-Alternating-Subarray-Sum.cpp
1 parent 62e453b commit 93f2601

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using LL = long long;
2+
class Solution {
3+
public:
4+
long long maximumAlternatingSubarraySum(vector<int>& nums)
5+
{
6+
LL ret = INT_MIN;
7+
LL curSum0 = INT_MIN;
8+
LL curSum1 = 0;
9+
10+
for (LL x: nums)
11+
{
12+
LL curSum0_temp = curSum0;
13+
LL curSum1_temp = curSum1;
14+
curSum0 = max(curSum1_temp + x, x);
15+
curSum1 = curSum0_temp - x;
16+
17+
ret = max(ret, curSum0);
18+
ret = max(ret, curSum1);
19+
}
20+
return ret;
21+
}
22+
};

0 commit comments

Comments
 (0)