Stone Game - Problem
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.
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 pileOutput: 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
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
⚠ Quadratic Growth
Space Complexity
O(n)
Recursion depth is at most n levels deep
⚡ Linearithmic Space
Constraints
- 2 <= piles.length <= 500
- piles.length is even
- 1 <= piles[i] <= 500
- The sum of piles[i] is odd
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code