Skip to content

Latest commit

 

History

History
65 lines (47 loc) · 1.73 KB

_3196. Maximize Total Cost of Alternating Subarrays.md

File metadata and controls

65 lines (47 loc) · 1.73 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Completed during Weekly Contest 403 (q3)

Back to top


First completed : July 07, 2024

Last updated : July 07, 2024


Related Topics : Array, Dynamic Programming

Acceptance Rate : 28.75 %


Solutions

Python

class Solution:
    def maximumTotalCost(self, nums: List[int]) -> int:
        dp = [0, 0] + [0] * (len(nums))

        for i in range(0, len(nums)) :
            val1 = dp[i + 1] + nums[i]
            val2 = dp[i] + nums[i - 1] - nums[i]
            if i == 0 :
                val2 = -inf

            dp[i + 2] = max(val1, val2)

        return dp[-1]
class Solution:
    def maximumTotalCost(self, nums: List[int]) -> int:
        dp = [0, 0] + [0] * (len(nums))

        for i in range(0, len(nums)) :
            val1 = dp[i + 1] + nums[i]
            if i == 0 :
                val1 = nums[i]

            val2 = dp[i] + nums[i - 1] - nums[i]
            if i == 0 :
                val2 = -inf
            elif i == 1 :
                val2 = nums[i - 1] - nums[i]
            dp[i + 2] = max(val1, val2)

        return dp[-1]