Maximize Total Cost of Alternating Subarrays Problem
Description
LeetCode Problem 3196.
You are given an integer array nums with length n.
The cost of a subarray nums[l..r], where 0 <= l <= r < n, is defined as:
cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (−1)^(r − l)
Your task is to split nums into subarrays such that the total cost of the subarrays is maximized, ensuring each element belongs to exactly one subarray.
Formally, if nums is split into k subarrays, where k > 1, at indices i_1, i_2, ..., i_(k − 1), where 0 <= i_1 < i_2 < ... < i_(k - 1) < n - 1, then the total cost will be:
cost(0, i_1) + cost(i_1 + 1, i_2) + ... + cost(i_(k − 1) + 1, n − 1)
Return an integer denoting the maximum total cost of the subarrays after splitting the array optimally.
Note: If nums is not split into subarrays, i.e. k = 1, the total cost is simply cost(0, n - 1).
Example 1:
Input: nums = [1,-2,3,4]
Output: 10
Explanation:
One way to maximize the total cost is by splitting [1, -2, 3, 4] into subarrays [1, -2, 3] and [4]. The total cost will be (1 + 2 + 3) + 4 = 10.
Example 2:
Input: nums = [1,-1,1,-1]
Output: 4
Explanation:
One way to maximize the total cost is by splitting [1, -1, 1, -1] into subarrays [1, -1] and [1, -1]. The total cost will be (1 + 1) + (1 + 1) = 4.
Example 3:
Input: nums = [0]
Output: 0
Explanation:
We cannot split the array further, so the answer is 0.
Example 4:
Input: nums = [1,-1]
Output: 2
Explanation:
Selecting the whole array gives a total cost of 1 + 1 = 2, which is the maximum.
Constraints:
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Solution
Approach: Memoized Recursion
Splitting the array into subarrays means each element is either added, or subtracted when it sits at an odd position inside its subarray. Equivalently: after adding an element, the next one may be added or subtracted; after subtracting an element, the next one must be added (it starts a new subarray, or sits at an even position).
Define dfs(i, j) as the best total from index i onward, where j = 1 means element i is allowed to be subtracted and j = 0 means it must be added. The answer is dfs(0, 0).
- If
iis past the end, return0. - Add
nums[i]:nums[i] + dfs(i + 1, 1). - If
j = 1, also try subtracting it:-nums[i] + dfs(i + 1, 0). Take the larger.
Cache each (i, j) so every state is computed once.
Complexity: O(n) time and O(n) space.
from functools import lru_cache
class Solution:
def maximumTotalCost(self, nums: List[int]) -> int:
@lru_cache(None)
def dfs(i: int, j: int) -> int:
if i >= len(nums):
return 0
ans = nums[i] + dfs(i + 1, 1)
if j == 1:
ans = max(ans, -nums[i] + dfs(i + 1, 0))
return ans
return dfs(0, 0)