Stone Game III - Problem

Welcome to another thrilling round of Alice and Bob's Stone Game! ๐ŸŽฎ

In this strategic battle, Alice and Bob face a row of stones, each carrying a specific point value (which can be positive, negative, or zero). The twist? Players must take stones from the beginning of the row, and they can grab 1, 2, or 3 stones per turn.

Game Rules:

  • Alice always goes first
  • Players alternate turns
  • On each turn, a player must take 1, 2, or 3 stones from the front
  • Each player's score = sum of values of stones they collected
  • Both players play optimally (always make the best possible move)

Your mission: Determine who wins when both players play perfectly! Return "Alice" if Alice wins, "Bob" if Bob wins, or "Tie" if they end with equal scores.

Example: For stones [1,2,3,7], Alice takes 3 stones (1+2+3=6), Bob takes the last stone (7), Bob wins with 7 > 6.

Input & Output

example_1.py โ€” Basic Game
$ Input: [1,2,3,7]
โ€บ Output: Bob
๐Ÿ’ก Note: Alice takes 3 stones (1+2+3=6), Bob takes the remaining stone (7). Bob wins 7 > 6.
example_2.py โ€” Negative Values
$ Input: [1,2,3,-9]
โ€บ Output: Alice
๐Ÿ’ก Note: Alice takes 3 stones (1+2+3=6), Bob is forced to take -9. Alice wins 6 > -9.
example_3.py โ€” Tie Game
$ Input: [1,2,3,6]
โ€บ Output: Tie
๐Ÿ’ก Note: Alice takes 3 stones (1+2+3=6), Bob takes 1 stone (6). Both end with score 6, it's a tie.

Visualization

Tap to expand
Stone Game III: Minimax StrategyInitial State: [1, 2, 3, 7] - Alice's Turn1237Alice's Options:Option 1: Take 1 stoneAlice gets: 1Bob plays: [2,3,7]Alice advantage: 1 - 12 = -11Not optimalOption 2: Take 2 stonesAlice gets: 1+2 = 3Bob plays: [3,7]Alice advantage: 3 - 10 = -7Better but not bestOption 3: Take 3 stones โœ“Alice gets: 1+2+3 = 6Bob gets: [7]Alice advantage: 6 - 7 = -1OPTIMAL CHOICEFinal Result: Bob wins (7 > 6)Key Insight: Track Score Differenceโ€ข Instead of tracking both players' scores separatelyโ€ข Track maximum advantage current player can achieveโ€ข Advantage = stones_taken - opponent's_best_responseโ€ข Memoization ensures each position is calculated only once: O(n)
Understanding the Visualization
1
Current Player's Turn
Consider taking 1, 2, or 3 stones from the front
2
Score Current Move
Calculate the sum of stones taken in this move
3
Opponent's Response
Assume opponent plays optimally from remaining stones
4
Maximize Advantage
Choose the move that gives maximum score difference
5
Cache Result
Store result to avoid recalculating this position
Key Takeaway
๐ŸŽฏ Key Insight: Transform the two-player problem into a single-player optimization by tracking score differences. Use memoization for optimal O(n) performance.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each position is calculated exactly once, with O(1) work per position

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Memoization cache stores result for each position, plus recursion stack

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค stoneValue.length โ‰ค 5 ร— 104
  • -1000 โ‰ค stoneValue[i] โ‰ค 1000
  • Both players play optimally
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 25
89.2K Views
Medium-High Frequency
~18 min Avg. Time
1.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