There was an error while loading. Please reload this page.
1 parent 62e453b commit 93f2601Copy full SHA for 93f2601
Dynamic_Programming/2036.Maximum-Alternating-Subarray-Sum/2036.Maximum-Alternating-Subarray-Sum.cpp
@@ -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