#3196
Maximize Total Cost of Alternating Subarrays
expert · 1130 · lc medium +32 · 29.6% accepted · 194 likes · top 7%
Description
You are given an integer array nums of length n.
Define the cost of a subarray nums[l..r] as:cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (-1)^(r - l)
Partition nums into non-overlapping subarrays (each element in exactly one) to maximize the total cost. If k = 1 (no splits), the total cost is simply cost(0, n - 1).
Return the maximum total cost.
Code
1
2
3