1911. Maximum Alternating Subsequence Sum
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 59% Topics: Array, Dynamic Programming
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def maxAlternatingSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result = nums[0]
for i in xrange(len(nums)-1):
result += max(nums[i+1]-nums[i], 0)
return result
Solution from kamyu104/LeetCode-Solutions · MIT