Stone Game is a classic two-player strategy game that tests your understanding of dynamic programming and game theory.

Alice and Bob are playing a competitive game with piles of stones arranged in a row. There are an even number of piles, and each pile contains a positive number of stones. The goal is simple: collect the most stones to win!

Here's how the game works:
• Alice goes first (advantage!)
• Players alternate turns
• On each turn, a player must take an entire pile from either the beginning or end of the row
• The game continues until all piles are taken
• The player with the most stones wins

Since the total number of stones is odd, there are no ties possible. Both players play optimally, meaning they always make the best possible move.

Input: An array piles where piles[i] is the number of stones in the i-th pile
Output: true if Alice wins when both players play optimally, false otherwise

Input & Output

example_1.py — Basic Case
$ Input: [5,3,4,5]
Output: true
💡 Note: Alice can win by taking 5 (index 0), then Bob takes 3, Alice takes 5 (index 3), Bob takes 4. Alice gets 5+5=10, Bob gets 3+4=7. Alice wins!
example_2.py — Another Win
$ Input: [3,7,2,3]
Output: true
💡 Note: Alice takes 3 (right), Bob takes 3 (left), Alice takes 7, Bob takes 2. Alice gets 3+7=10, Bob gets 3+2=5. Alice wins again!
example_3.py — Always True
$ Input: [1,2,3,4,5,6]
Output: true
💡 Note: With any even number of piles, Alice can always guarantee a win through optimal strategy, regardless of the actual values.

Visualization

Tap to expand
Stone Game: Alice's Guaranteed Win Strategy5371Even (0)Odd (1)Even (2)Odd (3)Even StrategyPositions 0,2: 5 + 7 = 12 stonesAlice can force this outcome!Odd StrategyPositions 1,3: 3 + 1 = 4 stonesAlice can force this too!Alice chooses Even Strategy (12 > 4)Result: Alice wins with 12 stones vs Bob's 4 stones!🎯 Key: Alice controls the game from her first move!
Understanding the Visualization
1
Game Setup
Even number of piles arranged in a row, players take from ends only
2
Position Control
Alice can guarantee either all odd positions or all even positions
3
Strategic Choice
She picks the strategy that gives her more stones
4
Guaranteed Victory
Since total is odd, one strategy must be better than the other
Key Takeaway
🎯 Key Insight: Alice always wins because she can choose at the start which 'color' of positions (odd or even) to collect throughout the game, and one color must have more stones than the other!

Time & Space Complexity

Time Complexity
⏱️
O(2^n)

Each turn has 2 choices, leading to 2^n possible game sequences

n
2n
Quadratic Growth
Space Complexity
O(n)

Recursion depth is at most n levels deep

n
2n
Linearithmic Space

Constraints

  • 2 <= piles.length <= 500
  • piles.length is even
  • 1 <= piles[i] <= 500
  • The sum of piles[i] is odd
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
78.5K Views
Medium Frequency
~15 min Avg. Time
3.8K 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