Maximize Total Cost of Alternating Subarrays - Problem
Maximize Total Cost of Alternating Subarrays

You are given an integer array nums of length n. Your goal is to strategically split this array into subarrays to maximize the total cost.

The cost of a subarray nums[l..r] (where 0 โ‰ค l โ‰ค r < n) follows an alternating pattern:
cost(l, r) = nums[l] - nums[l + 1] + nums[l + 2] - nums[l + 3] + ...

In other words, we add elements at even positions relative to the start and subtract elements at odd positions.

Your Task: Split nums into one or more subarrays such that each element belongs to exactly one subarray and the total cost is maximized.

Example: For array [1, -2, 3, 4], you could split it as [1, -2] and [3, 4] giving costs (1 - (-2)) + (3 - 4) = 3 + (-1) = 2.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, -2, 3, 4]
โ€บ Output: 10
๐Ÿ’ก Note: We can split the array as [1, -2, 3] + [4]. The first subarray gives cost 1 - (-2) + 3 = 6, and the second gives cost 4. Total: 6 + 4 = 10.
example_2.py โ€” Single Element
$ Input: [5]
โ€บ Output: 5
๐Ÿ’ก Note: With only one element, the cost is simply 5 (no alternating pattern needed).
example_3.py โ€” Negative Values
$ Input: [-1, -2, -3]
โ€บ Output: -1
๐Ÿ’ก Note: Best strategy is to split as [-1] + [-2] + [-3], giving costs -1 + (-2) + (-3) = -6. Actually, the optimal is to use [-1, -2, -3] as one subarray: -1 - (-2) + (-3) = -1 + 2 - 3 = -2. But splitting as [-1] + [-2] + [-3] gives -1 + (-2) + (-3) = -6. The optimal is actually [-1] which gives -1.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • -109 โ‰ค nums[i] โ‰ค 109
  • The array must be split such that each element belongs to exactly one subarray

Visualization

Tap to expand
Trading Sessions with Alternating RulesStock Prices: [1, -2, 3, 4]$1BUY$-2SELL$3BUY$4BUYTrading Sessions:Session 1Buy $1 โ†’ Sell $-2 โ†’ Buy $3Profit: 1 - (-2) + 3 = 6Session 2Buy $4Profit: 4Alternative Strategy:Single SessionBuy $1 โ†’ Sell $-2 โ†’ Buy $3 โ†’ Sell $4Profit: 1 - (-2) + 3 - 4 = 2Decision Tree:1-23ContinueNew SessionOptimal StrategySplit: [1, -2, 3] + [4]Total Profit: 6 + 4 = 10Sessions: 2
Understanding the Visualization
1
Initialize Trading
Start first trading session by buying the first stock
2
Make Decisions
At each stock, decide: continue current session or start new session
3
Track States
Maintain maximum profit for both 'just bought' and 'just sold' states
4
Optimal Choice
Choose the action that maximizes total profit across all sessions
Key Takeaway
๐ŸŽฏ Key Insight: At each position, we can either continue the current trading session (alternating buy/sell) or start a new session (always begins with buy). Dynamic programming helps us track the maximum profit for both states and make optimal decisions.
Asked in
Google 12 Meta 8 Amazon 6 Microsoft 4
43.7K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen