In Stone Game III, Alice and Bob take turns taking 1, 2, or 3 piles from the beginning of the row. The total score is the sum of the stones in the piles taken. The game ends when no more piles are left. The player with the highest score wins. You need to return "Alice", "Bob", or "Tie".
This "Hard" problem tests your ability to implement the Minimax strategy using Dynamic Programming. It's more straightforward than Stone Game II because the number of choices is fixed (1, 2, or 3), but it still requires careful handling of relative scores. It assesses your ability to think about "optimal play" in a competitive setting.
The pattern is Dynamic Programming (Linear). We define dp[i] as the maximum relative score (Alice's score - Bob's score) the current player can achieve starting from index .
From index , the player can:
relative_score = piles[i] - dp[i+1]relative_score = piles[i] + piles[i+1] - dp[i+2]relative_score = piles[i] + piles[i+1] + piles[i+2] - dp[i+3]
The player will choose the maximum of these three options.Piles: [1, 2, 3, 7].
dp[3] = 7.dp[2] = 10.dp[1] = 12.dp[0] > 0, Alice wins.For zero-sum games, tracking the difference between scores (myScore - yourScore) is usually much simpler than tracking two separate scores. This is a key insight for Game Theory, Dynamic Programming interview pattern questions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Stone Game V | Hard | Solve | |
| Stone Game | Medium | Solve | |
| Stone Game VII | Medium | Solve | |
| Stone Game VIII | Hard | Solve | |
| Stone Game II | Medium | Solve |