1406. Stone Game III
hardAlice and Bob alternate taking 1, 2, or 3 piles from the front; stone values can be negative. Suffix DP tracking the score-difference advantage of the player to move.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue. Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2, or 3 stones from the first remaining stones in the row. The score of each player is the sum of values of the stones taken. The score of each player is 0 initially. The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken. Assume Alice and Bob play optimally. Return "Alice" if Alice will win, "Bob" if Bob will win, or "Tie" if they will end the game with the same score.
Constraints
1 <= stoneValue.length <= 5 * 10^4-1000 <= stoneValue[i] <= 1000
Examples
Example 1
stoneValue = [1,2,3,7]"Bob"Explanation: Alice will always lose. Her best move will be to take three piles and the score becomes 6. Now the score of Bob is 7 and Bob wins.
Example 2
stoneValue = [1,2,3,-9]"Alice"Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score. If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. In the next move, Alice will take the pile with value = -9 and lose. If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. In the next move, Alice will take the pile with value = -9 and her score becomes -6. The next move, Bob's score will be 0 and Bob wins.
Example 3
stoneValue = [1,2,3,6]"Tie"Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Hints
Progressive — try the first before opening the next.
Hint 1
dp[i] = the score difference (current player - opponent) achievable starting at index i.
Hint 2
Sweep i from n-1 down to 0. For each i try x in {1, 2, 3}: dp[i] = max(dp[i], sum(stoneValue[i..i+x-1]) - dp[i+x]).
Hint 3
Final answer: compare dp[0] to 0 to decide Alice/Bob/Tie.
Hint 4
1D DP (not 2D) is enough here because the only state is the start index — chose to include in dp-2d for the game-DP family.
Solution approach
Reveal approach
Suffix DP. dp[i] is the maximum (current player - opponent) score difference achievable from stoneValue[i..]. Sweep i from n - 1 down to 0. At each i, consider taking x = 1, 2, or 3 stones (when in range). For each x, the current player gains stoneValue[i] + ... + stoneValue[i + x - 1] and the opponent will then play optimally, yielding dp[i + x] for them — flip the sign because dp is from the mover's perspective. So dp[i] = max over x of (sum of taken stones) - dp[i + x]. Final answer: dp[0] > 0 -> Alice; < 0 -> Bob; == 0 -> Tie. O(n) time, O(n) space. Listed here for the game-DP series.
Complexity
- Time
- O(n)
- Space
- O(n)
Related patterns
- dynamic-programming
- game-theory
Related problems
- 877. Stone Game
- 1140. Stone Game II
- 1510. Stone Game IV
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
Practice these live with InterviewChamp.AI
Drill Stone Game III and 2D Dynamic Programming problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →