Alice and Bob take turns playing a game, with Alice starting first.

There are n stones arranged in a row. On each player's turn, while the number of stones is more than one, they will do the following:

  • Choose an integer x > 1, and remove the leftmost x stones from the row.
  • Add the sum of the removed stones' values to the player's score.
  • Place a new stone, whose value is equal to that sum, on the left side of the row.

The game stops when only one stone is left in the row.

The score difference between Alice and Bob is (Alice's score - Bob's score). Alice's goal is to maximize the score difference, and Bob's goal is to minimize the score difference.

Given an integer array stones of length n where stones[i] represents the value of the ith stone from the left, return the score difference between Alice and Bob if they both play optimally.

Input & Output

Example 1 — Small Array
$ Input: stones = [-1,2,-3,4,-5]
Output: 5
💡 Note: Alice can take the first 4 stones (sum = -1+2+(-3)+4 = 2), then Bob must take the remaining stones [2,-5] (sum = -3), giving Alice advantage of 2-(-3) = 5.
Example 2 — All Positive
$ Input: stones = [7,-6,5,10,5,-2,-6]
Output: 13
💡 Note: Alice plays optimally to maximize score difference, achieving a final advantage of 13.
Example 3 — Minimum Size
$ Input: stones = [-10,20]
Output: 10
💡 Note: Alice must take both stones (sum = 10), Bob gets nothing, so difference is 10.

Constraints

  • 3 ≤ stones.length ≤ 104
  • -104 ≤ stones[i] ≤ 104

Visualization

Tap to expand
Stone Game VIII - Dynamic Programming INPUT stones array (n=5) -1 i=0 2 i=1 -3 i=2 4 i=3 -5 i=4 Prefix Sums -1 1 -2 2 -3 Game Rules: - Remove x leftmost stones (x>1) - Add sum to player's score - Place new stone with sum - Alice: maximize diff - Bob: minimize diff stones = [-1, 2, -3, 4, -5] ALGORITHM STEPS 1 Compute Prefix Sum pre[i] = sum of stones[0..i] 2 Define DP State dp[i] = max diff if taking i+ 3 DP Transition dp[i] = max(dp[i+1], pre[i]-dp[i+1]) 4 Iterate Backwards From n-2 to 1, return dp[1] DP Table (right to left) i=4 i=3 i=2 i=1 -3 2 2 5 Optimization Track suffix max to reduce O(n^2) to O(n) FINAL RESULT Optimal Game Play Alice's Turn Takes first 2 stones: -1+2 = 1 Bob's Turn Takes 2 stones: 1+(-3) = -2 Alice's Turn Takes 2 stones: -2+4 = 2 Bob's Turn Takes 2 stones: 2+(-5) = -3 Alice 1 + 2 = 3 Bob -2+(-3)=-5 Score Difference 3 - (-5) = 5 Key Insight: When player takes stones 0 to i, they get prefix[i]. The next player faces the same game but smaller. dp[i] = max score difference when current player can take at least (i+1) stones from left. Transition: dp[i] = max(dp[i+1], prefix[i] - dp[i+1]) -- either skip or take, optimizing suffix max. TutorialsPoint - Stone Game VIII | Dynamic Programming Approach Time: O(n) Space: O(n)
Asked in
Google 12 Amazon 8 Microsoft 6
28.5K Views
Medium Frequency
~35 min Avg. Time
892 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