2036. Maximum Alternating Subarray Sum

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2036

class Solution:
    def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
        res = f = g = -inf
        for x in nums:
            f, g = max(g, 0) + x, f - x
            res = max(res, f, g)
        return res