Stone Game VII - Problem
Stone Game VII is a strategic two-player game where Alice and Bob compete for the highest score by removing stones from the ends of a row.

šŸŽ® Game Rules:
• Players alternate turns, with Alice going first
• On each turn, a player removes either the leftmost or rightmost stone
• The player's score increases by the sum of all remaining stones after removal
• The game ends when no stones remain

šŸŽÆ The Twist: Bob knows he'll lose, so he plays to minimize the score difference. Alice wants to maximize the difference.

Your Task: Given an array stones where stones[i] is the value of the i-th stone, return the maximum score difference Alice can achieve when both players play optimally.

Example: With stones [5,3,1,4,2], Alice can achieve a score difference of 6 through optimal play.

Input & Output

example_1.py — Python
$ Input: [5,3,1,4,2]
› Output: 6
šŸ’” Note: Alice removes 5, remaining sum = 10, Alice scores 10. Bob removes 2, remaining sum = 6, Bob scores 6. Alice removes 4, remaining sum = 4, Alice scores 4. Bob removes 1, remaining sum = 3, Bob scores 3. Alice removes 3, game ends. Alice total = 14, Bob total = 9, difference = 5. But with optimal play, Alice can achieve difference of 6.
example_2.py — Python
$ Input: [7,90,5,1,100,10,10,2]
› Output: 122
šŸ’” Note: In this larger game, Alice can achieve a score difference of 122 through optimal stone removal strategy.
example_3.py — Python
$ Input: [1,2]
› Output: 1
šŸ’” Note: Alice removes 1, gets score 2. Bob removes 2, gets score 0. Difference is 2-0=2. If Alice removes 2 first, she gets score 1, Bob gets 0. Alice chooses the first option for difference of 2. Wait - let me recalculate: Alice removes stone worth 1, remaining stones have sum 2, so Alice scores 2. Bob removes stone worth 2, no stones remain, Bob scores 0. Alice advantage = 2-0 = 2. Actually, if Alice removes 2 first, remaining sum is 1, Alice scores 1. Bob removes 1, remaining sum 0, Bob scores 0. Alice advantage = 1-0 = 1. So Alice chooses first option, but the correct answer should be 1 based on optimal play from both sides.

Constraints

  • n == stones.length
  • 2 ≤ n ≤ 1000
  • 1 ≤ stones[i] ≤ 1000
  • Both players play optimally - Alice maximizes difference, Bob minimizes it

Visualization

Tap to expand
Stone Game VII - Strategic Gameplay53142Initial stones: [5, 3, 1, 4, 2]Alice's TurnRemove stone 5Score: 3+1+4+2 = 10Alice's TurnRemove stone 2Score: 5+3+1+4 = 1331425314Bob's ResponseMinimizes Alice's advantageChooses optimal counter-moveAlice's StrategyMaximize Score DifferenceBob's StrategyMinimize Score DifferenceDynamic Programming finds optimal moves for both playersTime: O(n²) | Space: O(n²) with memoization
Understanding the Visualization
1
Initial Setup
Players face a row of stones, can only take from ends
2
Score Calculation
When removing a stone, player scores the sum of all remaining stones
3
Optimal Strategy
Alice maximizes score difference, Bob minimizes it
4
DP Solution
Use memoization to efficiently explore all possible game states
Key Takeaway
šŸŽÆ Key Insight: This is a classic minimax game theory problem where we use dynamic programming to efficiently explore all possible game states, with each player making optimal decisions from their perspective.
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
32.0K Views
Medium Frequency
~25 min Avg. Time
850 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