Tensorum
LeetCode 877 · Game DP

Stone Game

LeetCode 877 · Medium · solution, intuition & step-by-step walkthrough

Minimax on (i, j, turn) — and with an even pile count Alice can always win.

MediumArrayMathDynamic ProgrammingMinimaxGame Theory
0 msruntime · beats 100%
19.2 MBmemory · beats 77%
2approaches, in order
4accepted runs · last 9 Apr 2026

This is how I solved Stone Game (LeetCode 877), a medium problem from the Game DP pattern. Below: the intuition, the approach step by step, my accepted Python code and the 2 versions I wrote on the way, animated test cases and a line-by-line debugger.

Intuition

Model the game as a score difference: Alice adds the pile she takes, Bob subtracts his. Alice maximises the difference, Bob minimises it, over the shrinking range of piles. Working it through shows Alice always wins with an even number of piles: she can choose to take all odd-indexed or all even-indexed piles, and one of those sets is larger.

Approach

  1. 1

    dfs(i, j, turn) = Alice-minus-Bob score for the piles i..j.

  2. 2

    Alice's turn: max of taking the left or right pile.

  3. 3

    Bob's turn: min, with the pile value subtracted.

  4. 4

    Alice wins if the full-range result is positive.

  5. 5

    Final version uses the parity argument and returns True.

How I solved it · from first idea to final

1
Minimax + memoO(n²) time · O(n²) space

lru_cache dfs(i, j, turn).

2
Math argumentAcceptedO(1)

Alice always wins.

Minimax + memoPythonO(n²) time · O(n²) space
from functools import lru_cache
class Solution:
    def stoneGame(self, piles: List[int]) -> bool:
        #Alice - Score positive - max_score we need
        #Bob - Score negative - min score we need
        n = len(piles)
        @lru_cache(maxsize=None)
        def dfs(i,j,turn):
            if i>j:
                return 0
            if turn=="Alice":
                from_left=piles[i]+dfs(i+1,j,"Bob")
                from_right=piles[j]+dfs(i,j-1,"Bob")
                return max(from_left,from_right)
            else:
                from_left=-piles[i]+dfs(i+1,j,"Alice")
                from_right=-piles[j]+dfs(i,j-1,"Alice")
                return min(from_left,from_right)
        return True if dfs(0,n-1,"Alice")>0 else False
Math argumentPythonO(1)
class Solution:
    def stoneGame(self, piles: List[int]) -> bool:
        return True
Loading the step-through…

Stone Game — FAQ

What is the intuition behind Stone Game?

Model the game as a score difference: Alice adds the pile she takes, Bob subtracts his. Alice maximises the difference, Bob minimises it, over the shrinking range of piles. Working it through shows Alice always wins with an even number of piles: she can choose to take all odd-indexed or all even-indexed piles, and one of those sets is larger.

How do you solve Stone Game (LeetCode 877)?

1. dfs(i, j, turn) = Alice-minus-Bob score for the piles i..j. 2. Alice's turn: max of taking the left or right pile. 3. Bob's turn: min, with the pile value subtracted. 4. Alice wins if the full-range result is positive. 5. Final version uses the parity argument and returns True.

What is the time and space complexity of Stone Game?

Time: O(1) final (O(n²) for minimax). Space: O(1). (Accepted approach: Math argument.)

What are common mistakes in Stone Game?

The 'return True' trick depends on an even pile count and an odd total — read the constraints before using it.

Is there more than one way to solve Stone Game?

Minimax + memo (O(n²) time · O(n²) space): lru_cache dfs(i, j, turn). Math argument (O(1)): Alice always wins.