Stone Game VII - Problem

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, they can remove either the leftmost stone or the rightmost stone from the row and receive points equal to the sum of the remaining stones' values in the row.

The winner is the one with the higher score when there are no stones left to remove.

Bob found that he will always lose this game (poor Bob, he always loses), so he decided to minimize the score's difference. Alice's goal is to maximize the difference in the score.

Given an array of integers stones where stones[i] represents the value of the ith stone from the left, return the difference in Alice and Bob's score if they both play optimally.

Input & Output

Example 1 — Basic Game
$ Input: stones = [5,3,1,4,2]
Output: 6
💡 Note: Alice starts and plays optimally. One optimal sequence: Alice removes 2 (scores 5+3+1+4=13), Bob removes 5 (scores 3+1+4=8), Alice removes 4 (scores 3+1=4), Bob removes 1 (scores 3), Alice takes 3 (scores 0). Alice: 13+4=17, Bob: 8+3=11. Difference: 17-11=6.
Example 2 — Small Array
$ Input: stones = [7,90,5,1,100,10,10,2]
Output: 122
💡 Note: With optimal play from both players, Alice can achieve a score difference of 122 points.
Example 3 — Minimum Size
$ Input: stones = [3,7]
Output: 7
💡 Note: Alice can remove 3 (gets score 7) or remove 7 (gets score 3). She chooses to remove 3, then Bob must take 7 (gets score 0). Alice: 7, Bob: 0. Difference: 7-0=7.

Constraints

  • n == stones.length
  • 2 ≤ n ≤ 1000
  • 1 ≤ stones[i] ≤ 1000

Visualization

Tap to expand
Stone Game VII - Dynamic Programming INPUT Stones arranged in a row: 5 3 1 4 2 i=0 i=1 i=2 i=3 i=4 stones = [5,3,1,4,2] n = 5 stones Total sum = 15 Alice starts first Game Rules: - Remove left OR right stone - Score = sum of remaining - Alice: maximize diff - Bob: minimize diff ALGORITHM STEPS 1 Compute Prefix Sum Quick range sum queries 2 Define DP State dp[i][j] = max diff for [i,j] 3 DP Transition Choose left or right removal 4 Return dp[0][n-1] Answer for full array DP Recurrence: dp[i][j] = max( sum(i+1,j) - dp[i+1][j], sum(i,j-1) - dp[i][j-1]) Sample DP values: dp [0,4] = 6 FINAL RESULT Optimal Play Sequence: Turn 1 (Alice): Remove 2, Score: 13 Turn 2 (Bob): Remove 5, Score: 8 Turn 3 (Alice): Remove 4, Score: 4 Turn 4 (Bob): Remove 1, Score: 3 Turn 5 (Alice): Remove 3, Score: 0 Final Scores: Alice: 17 | Bob: 11 Difference: 17 - 11 = 6 Output: 6 Key Insight: The game is zero-sum relative to remaining stones. Using DP, dp[i][j] represents the maximum score difference the current player can achieve for stones[i..j]. The current player's gain becomes the opponent's loss, so we subtract dp of the remaining subproblem. Time: O(n^2), Space: O(n^2). TutorialsPoint - Stone Game VII | Dynamic Programming Approach
Asked in
Google 12 Amazon 8 Facebook 6
23.4K Views
Medium Frequency
~25 min Avg. Time
847 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