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 12, 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.

Examples

Example 1

1
2
3
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

1
2
3
4
5
6
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

1
2
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] <= 1000

Solution

Method 1 – Dynamic Programming (Optimal Play)

Intuition

We can use DP to compute the best score difference Alice can achieve from each position, considering all options for taking 1, 2, or 3 stones. The winner is determined by the final score difference.

Approach

  1. Let dp[i] be the maximum score difference Alice can achieve starting from index i.
  2. For each position, try taking 1, 2, or 3 stones and subtract the result of the next state.
  3. The answer is determined by the sign of dp[0].

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    string stoneGameIII(vector<int>& stoneValue) {
        int n = stoneValue.size();
        vector<int> dp(n+1);
        for (int i = n-1; i >= 0; --i) {
            int take = 0, best = INT_MIN;
            for (int k = 0; k < 3 && i+k < n; ++k) {
                take += stoneValue[i+k];
                best = max(best, take - dp[i+k+1]);
            }
            dp[i] = best;
        }
        if (dp[0] > 0) return "Alice";
        if (dp[0] < 0) return "Bob";
        return "Tie";
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
func stoneGameIII(stoneValue []int) string {
    n := len(stoneValue)
    dp := make([]int, n+1)
    for i := n-1; i >= 0; i-- {
        take, best := 0, -1<<31
        for k := 0; k < 3 && i+k < n; k++ {
            take += stoneValue[i+k]
            if take-dp[i+k+1] > best { best = take-dp[i+k+1] }
        }
        dp[i] = best
    }
    if dp[0] > 0 { return "Alice" }
    if dp[0] < 0 { return "Bob" }
    return "Tie"
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
    public String stoneGameIII(int[] stoneValue) {
        int n = stoneValue.length;
        int[] dp = new int[n+1];
        for (int i = n-1; i >= 0; --i) {
            int take = 0, best = Integer.MIN_VALUE;
            for (int k = 0; k < 3 && i+k < n; ++k) {
                take += stoneValue[i+k];
                best = Math.max(best, take - dp[i+k+1]);
            }
            dp[i] = best;
        }
        if (dp[0] > 0) return "Alice";
        if (dp[0] < 0) return "Bob";
        return "Tie";
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
    fun stoneGameIII(stoneValue: IntArray): String {
        val n = stoneValue.size
        val dp = IntArray(n+1)
        for (i in n-1 downTo 0) {
            var take = 0
            var best = Int.MIN_VALUE
            for (k in 0..2) {
                if (i+k < n) {
                    take += stoneValue[i+k]
                    best = maxOf(best, take - dp[i+k+1])
                }
            }
            dp[i] = best
        }
        return when {
            dp[0] > 0 -> "Alice"
            dp[0] < 0 -> "Bob"
            else -> "Tie"
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution:
    def stoneGameIII(self, stoneValue: list[int]) -> str:
        n = len(stoneValue)
        dp = [0]*(n+1)
        for i in range(n-1, -1, -1):
            take, best = 0, float('-inf')
            for k in range(3):
                if i+k < n:
                    take += stoneValue[i+k]
                    best = max(best, take - dp[i+k+1])
            dp[i] = best
        if dp[0] > 0: return "Alice"
        if dp[0] < 0: return "Bob"
        return "Tie"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
impl Solution {
    pub fn stone_game_iii(stone_value: Vec<i32>) -> String {
        let n = stone_value.len();
        let mut dp = vec![0; n+1];
        for i in (0..n).rev() {
            let mut take = 0;
            let mut best = i32::MIN;
            for k in 0..3 {
                if i+k < n {
                    take += stone_value[i+k];
                    best = best.max(take - dp[i+k+1]);
                }
            }
            dp[i] = best;
        }
        if dp[0] > 0 { "Alice".to_string() }
        else if dp[0] < 0 { "Bob".to_string() }
        else { "Tie".to_string() }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    stoneGameIII(stoneValue: number[]): string {
        const n = stoneValue.length;
        const dp = Array(n+1).fill(0);
        for (let i = n-1; i >= 0; i--) {
            let take = 0, best = -Infinity;
            for (let k = 0; k < 3; k++) {
                if (i+k < n) {
                    take += stoneValue[i+k];
                    best = Math.max(best, take - dp[i+k+1]);
                }
            }
            dp[i] = best;
        }
        if (dp[0] > 0) return "Alice";
        if (dp[0] < 0) return "Bob";
        return "Tie";
    }
}

Complexity

  • ⏰ Time complexity: O(n) where n is the number of stones. Each state is computed in constant time.
  • 🧺 Space complexity: O(n) for the DP array.