Problem Description
Given an integer array nums, you need to split it into one or more contiguous subarrays so that every element belongs to exactly one subarray. For any subarray nums[l..r], its cost is defined as:
cost(l, r) = nums[l] - nums[l+1] + nums[l+2] - …, with signs alternating (starting with a plus).
When the array is split into subarrays, the total cost is the sum of the costs of each subarray. The goal is to choose the splitting positions to maximize the total cost. If no split is made (i.e. the entire array is one subarray), the cost is simply cost(0, n – 1).
Key Insights
- The cost of a subarray is computed as an alternating sum, where every subarray always “starts fresh” with a plus sign.
- If the whole array is taken without splitting, the sign at position i is determined solely by whether i is even (plus) or odd (minus).
- Making a split resets the sign pattern. In particular, an element that falls in an odd position in the unsplit alternating sum (and thus is subtracted) can become the first element of a new subarray (and be added), thereby “flipping” its contribution.
- The problem can be solved with dynamic programming by considering the best split ending at each index and maintaining two helper values – one corresponding to potential subarray-start candidates at even positions and one for odd positions.
- By rewriting the cost of a subarray from index j to i in terms of a precomputed alternating prefix sum A[i] = Σ[k=0..i] ((-1)^k * nums[k]), we obtain: dp[i] = max over j=0..i of { dp[j-1] + (-1)^j * (A[i] - (j > 0 ? A[j-1] : 0)) }.
- Rearranging the recurrence leads to a formulation where, for each index i, the best answer is computed as the maximum of two candidate formulas: Candidate1: A[i] + bestEven (for j even) Candidate2: -A[i] + bestOdd (for j odd)
- As we iterate over i from 0 to n – 1 we update our dp value and update the auxiliary variables (bestEven and bestOdd) for use in future transitions.
Space and Time Complexity
Time Complexity: O(n) – We process the array in a single pass. Space Complexity: O(1) – Only constant extra variables are needed (besides input).
Solution
We solve the problem using dynamic programming. We define dp[i] as the maximum total cost for optimally splitting the subarray nums[0..i]. By expressing the cost of the last subarray (which always starts with a plus sign) in terms of the alternating prefix sum A[i], we deduce that: dp[i] = max( A[i] + bestEven, - A[i] + bestOdd ) where: bestEven = maximum value of (dp[j-1] – A[j-1]) among candidates with j even (j represents a possible new subarray start) bestOdd = maximum value of (dp[j-1] + A[j-1]) among candidates with j odd. We initialize bestEven = 0 (corresponding to j = 0, where dp[-1] = 0 and A[-1] = 0) and bestOdd = -∞ (meaning no candidate yet). With each new index i, we update the alternating prefix sum A[i] based on the parity of i (using +nums[i] when i is even and -nums[i] when odd). Then we compute dp[i] and update the candidate for starting a new subarray at index i + 1: if (i+1) is even, the candidate is dp[i] – A[i]; if (i+1) is odd, the candidate is dp[i] + A[i]. This recurrence correctly “flips” the sign of an odd-indexed element when beneficial by starting a new subarray there and resetting the sign pattern.