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 the 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.
Example 1:
Input: stoneValue = [1,2,3,7] Output: "Bob" Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: stoneValue = [1,2,3,-9] Output: "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 also lose. Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: stoneValue = [1,2,3,6] Output: "Tie" Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Constraints:
1 <= stoneValue.length <= 5 * 104-1000 <= stoneValue[i] <= 1000Stone Game III is a classic game theory + dynamic programming problem where two players pick 1–3 stones per turn and try to maximize their total score. The key idea is to think in terms of the score difference between the current player and the opponent rather than tracking individual scores.
Use dynamic programming where dp[i] represents the maximum score difference the current player can achieve starting from index i. At each position, the player can take 1, 2, or 3 stones and add their values to the current score. The opponent then plays optimally from the next index, so their advantage is subtracted from the current player's gain.
The transition evaluates all valid choices and keeps the maximum resulting difference. Finally, the sign of dp[0] determines whether Alice wins, Bob wins, or the game ends in a tie. This approach efficiently models optimal play while avoiding exponential recursion.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Dynamic Programming (score difference from end) | O(n) | O(n) (can be optimized to O(1)) |
CrioDo
Use these hints if you're stuck. Try solving on your own first.
The game can be mapped to minmax game. Alice tries to maximize the total score and Bob tries to minimize it.
Use dynamic programming to simulate the game. If the total score was 0 the game is "Tie", and if it has positive value then "Alice" wins, otherwise "Bob" wins.
This approach involves using dynamic programming to evaluate possible outcomes from the end of the stone array to the beginning. This allows us to make optimal decisions at each step because we know the best scores for the rest of the game. The idea is to calculate the maximum score difference Alice can enforce using a suffix-based strategy.
Time Complexity: O(n), where n is the number of stones. Each stone position is computed once.
Space Complexity: O(n), where n is the number of stones. It uses an array of size n+1 to store computed values.
1public class Solution {
2 public string StoneGameIII(int[] stoneValue) {
3 int n = stoneValue.Length;
4 int[] dp = new int[n + 1];
5 dp[n] = 0;
6
7 for (int i = n - 1; i >= 0; i--) {
8 int take = 0;
9 dp[i] = int.MinValue;
10 for (int k = 0; k < 3 && i + k < n; k++) {
11 take += stoneValue[i + k];
12 dp[i] = Math.Max(dp[i], take - dp[i + k + 1]);
13 }
}
if (dp[0] > 0) return "Alice";
else if (dp[0] < 0) return "Bob";
else return "Tie";
}
}
This C# implementation employs an array of integers to retain intermediate results. It computes the score differential by iterating backward and determining the best possible pick of stones (1 to 3) at each step.
This approach leverages the recursive computation of outcomes with memoization. We recursively compute the score differences at each step, caching the results to avoid redundant calculations. At each stone index, the player's optimal score is determined by considering 1 to 3 stone choices.
Time Complexity: O(n), due to computation of score differences for unique indices once per index.
Space Complexity: O(n), from the recursive call stack and memoization cache storing outcomes for each state.
1def stoneGameIII(stoneValue):
2 from functools import lru_cache
3
4
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of game theory and dynamic programming problems like Stone Game III appear in FAANG and other top tech interviews. They test a candidate's ability to model optimal decisions, reason about state transitions, and design efficient DP solutions.
The optimal approach uses dynamic programming combined with game theory. Instead of tracking both players' scores, we compute the maximum score difference a player can achieve from each index. This allows us to simulate optimal decisions while keeping the solution efficient.
Using score difference simplifies the problem because it represents the advantage of the current player over the opponent. When the opponent plays next, their optimal score difference is subtracted from the current player's gain. This technique is common in competitive game DP problems.
A dynamic programming array is typically used to store the best score difference starting from each index. The array allows efficient bottom-up computation and avoids recomputing states. In optimized versions, only a few previous states are required, reducing space usage.
The Python solution employs a memoization decorator to cache previously computed results, optimizing recursive calls. This avoids recalculating results for subproblems, ensuring efficiency. Each call makes decisions based on potential stone selections and returns the best achievable score differential for Alice.