Skip to content

Commit 7717392

Browse files
authored
Create maximum-alternating-subsequence-sum.cpp
1 parent 220d5b1 commit 7717392

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
long long maxAlternatingSum(vector<int>& nums) {
7+
long long result = nums[0];
8+
for (int i = 0; i < size(nums) - 1; ++i) {
9+
result += max(nums[i + 1] - nums[i], 0);
10+
}
11+
return result;
12+
}
13+
};

0 commit comments

Comments
 (0)