Stone Game V - Problem
Stone Game V presents an exciting strategic challenge where Alice and Bob play an optimal game with stones arranged in a row.

Here's how it works:
Setup: Stones are arranged in a row, each with a specific value
Alice's Turn: She divides the row into two non-empty parts (left and right)
Bob's Turn: He calculates the sum of each part and throws away the part with higher sum
Tie Breaking: If sums are equal, Alice chooses which part to keep
Scoring: Alice gains points equal to the sum of the remaining part
Continue: The game continues with the remaining stones until only one stone is left

Your goal is to find the maximum score Alice can achieve when both players play optimally. This is a classic dynamic programming problem that requires thinking recursively about optimal subproblems.

Input & Output

example_1.py — Basic Game
$ Input: [6,2,3,4,7,9]
Output: 18
💡 Note: Alice can achieve maximum score 18. One optimal sequence: divide into [6] and [2,3,4,7,9] (sums 6 vs 25), Bob removes [2,3,4,7,9], Alice gets 6. Continue with remaining stones to get total score 18.
example_2.py — Equal Sums
$ Input: [7,7,7,7,7,7,7]
Output: 28
💡 Note: With all stones having equal value, Alice can always create equal sums and choose which part to keep, allowing her to maximize her total score across all rounds.
example_3.py — Two Stones Only
$ Input: [4,6]
Output: 4
💡 Note: Alice must split into [4] and [6]. Bob removes the larger sum [6], so Alice gets 4 points. Game ends since only one stone remains.

Constraints

  • 2 ≤ stoneValue.length ≤ 500
  • 1 ≤ stoneValue[i] ≤ 106
  • Array represents stones arranged in a row from left to right

Visualization

Tap to expand
Stone Game V: Alice vs Bob Strategy🏴‍☠️ The Treasure Division GameAlice (Strategic) divides treasure, Bob (Greedy) steals the larger pileAlice wants to maximize total treasure kept across all roundsAlice's TurnAnalyze all possibledivisions strategicallyBob's TurnAlways removes pilewith higher sumMemoizationCache results forrepeated subgames💡 Key Insight: Optimal SubstructureAlice's best score for any range of stones depends onthe optimal solutions for all possible smaller ranges🎯 Algorithm PerformanceTime: O(n³) • Space: O(n²) • Approach: DP + MemoizationPerfect for game theory problems with optimal substructure
Understanding the Visualization
1
Strategic Division
Alice analyzes all possible ways to split the stones, considering Bob's optimal response
2
Bob's Greedy Choice
Bob always removes the pile with higher sum (or Alice chooses if sums are equal)
3
Recursive Optimization
Apply the same strategy recursively to remaining stones
4
Memoization Magic
Cache results to avoid recalculating the same game states
Key Takeaway
🎯 Key Insight: Stone Game V is solved optimally using dynamic programming with memoization, where Alice's strategy for any range depends on optimal solutions for all possible sub-ranges created by her divisions.
Asked in
Google 15 Microsoft 12 Amazon 8 Meta 6
23.6K 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