🎮 Welcome to Stone Game II! Alice and Bob are back for another strategic stone-collecting battle. This time, the rules have evolved to create an even more engaging game of wits.

The Setup: There are several piles of stones arranged in a row, where piles[i] represents the number of stones in the i-th pile. Alice goes first, and both players want to maximize their own stone collection.

The Rules:

  • Players take turns, with Alice starting first
  • On each turn, a player can take all stones from the first X remaining piles, where 1 ≤ X ≤ 2M
  • After taking X piles, we update M = max(M, X)
  • Initially, M = 1
  • The game continues until all piles are taken

Your Goal: Assuming both Alice and Bob play optimally (making the best possible moves), determine the maximum number of stones Alice can collect.

Example: If piles = [2,7,9,4,4], Alice can strategically collect 10 stones while Bob gets 6 stones, even though Alice goes first and faces optimal opposition!

Input & Output

example_1.py — Basic Game
$ Input: piles = [2,7,9,4,4]
Output: 10
💡 Note: Alice starts with M=1. She can take 1 pile (gets 2) or 2 piles (gets 2+7=9). If she takes 2 piles, M becomes 2, and Bob can then take up to 4 piles from [9,4,4]. Bob will take all remaining piles (getting 17), leaving Alice with only 9. So Alice should take just 1 pile (gets 2), then Bob takes optimally, and through strategic play, Alice ends up with 10 stones total.
example_2.py — Small Array
$ Input: piles = [1,2,3,4,5,100]
Output: 104
💡 Note: Alice can strategically force the game so that she gets the pile with 100 stones plus some others. The optimal play leads to Alice collecting 104 stones while Bob gets the remaining stones.
example_3.py — Edge Case
$ Input: piles = [1]
Output: 1
💡 Note: With only one pile, Alice takes it all on her first turn since 1 ≤ 2×1. Alice gets 1 stone, Bob gets 0.

Visualization

Tap to expand
Stone Game II VisualizationAlice's Turn (M=1)27944Can take 1 or 2 piles (X ≤ 2M)Bob's Response (M=2)944Can take 1 to 4 piles (X ≤ 2×2)Memoization TableStateBest ScoreComputed(0,1)10(2,2)7(3,2)4Key Optimization: Suffix SumssuffixSum[i] = sum of all piles from position i to endAllows O(1) calculation: player_score = suffixSum[i] - opponent_best_from_remainingTime: O(n³) | Space: O(n²)Each of n×n states tries up to n moves. Memoization prevents recalculation.
Understanding the Visualization
1
Setup Phase
Alice and Bob face stone piles, M starts at 1
2
Strategic Thinking
Each player considers all legal moves (1 to 2M piles)
3
Advantage Calculation
Players choose moves that maximize their total minus opponent's total
4
Dynamic Adaptation
M grows with larger moves, opening up more possibilities
Key Takeaway
🎯 Key Insight: Transform the problem from "maximize my stones" to "maximize my advantage over opponent" using suffix sums and memoization to achieve optimal O(n³) solution.

Time & Space Complexity

Time Complexity
⏱️
O(n³)

We have O(n²) possible states (n positions × n values of M), and for each state we try O(n) moves

n
2n
Quadratic Growth
Space Complexity
O(n²)

Memoization table stores results for O(n²) states, plus O(n) for suffix sums

n
2n
Quadratic Space

Constraints

  • 1 ≤ piles.length ≤ 100
  • 1 ≤ piles[i] ≤ 104
  • Both players play optimally
  • Alice starts first
  • Initially M = 1
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 15
43.7K Views
Medium Frequency
~25 min Avg. Time
1.5K 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