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
Example: With stones
š® 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
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.
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code