Alice and Bob take turns playing a game, with Alice starting first.
There are n stones arranged in a row. On each player's turn, while the number of stones is more than one, they will do the following:
x > 1, and remove the leftmost x stones from the row.The game stops when only one stone is left in the row.
The score difference between Alice and Bob is (Alice's score - Bob's score). Alice's goal is to maximize the score difference, and Bob's goal is the minimize the score difference.
Given an integer array stones of length n where stones[i] represents the value of the ith stone from the left, return the score difference between Alice and Bob if they both play optimally.
Example 1:
Input: stones = [-1,2,-3,4,-5] Output: 5 Explanation: - Alice removes the first 4 stones, adds (-1) + 2 + (-3) + 4 = 2 to her score, and places a stone of value 2 on the left. stones = [2,-5]. - Bob removes the first 2 stones, adds 2 + (-5) = -3 to his score, and places a stone of value -3 on the left. stones = [-3]. The difference between their scores is 2 - (-3) = 5.
Example 2:
Input: stones = [7,-6,5,10,5,-2,-6] Output: 13 Explanation: - Alice removes all stones, adds 7 + (-6) + 5 + 10 + 5 + (-2) + (-6) = 13 to her score, and places a stone of value 13 on the left. stones = [13]. The difference between their scores is 13 - 0 = 13.
Example 3:
Input: stones = [-10,-12] Output: -22 Explanation: - Alice can only make one move, which is to remove both stones. She adds (-10) + (-12) = -22 to her score and places a stone of value -22 on the left. stones = [-22]. The difference between their scores is (-22) - 0 = -22.
Constraints:
n == stones.length2 <= n <= 105-104 <= stones[i] <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The game allows a player to take the first x stones and replace them with their sum, which is equivalent to selecting a prefix sum at index x - 1. Each chosen prefix sum adds to the current player's score and leaves the remaining prefix sums available for the other player. By utilizing dynamic programming backwards from the last possible choice, we can compute the maximum score difference each player can guarantee.
Here's how the algorithm would work step-by-step:
def stone_game_viii(stones: list[int]) -> int:
# Compute the prefix sums of the stone values
prefix_sums = [0] * len(stones)
prefix_sums[0] = stones[0]
for index in range(1, len(stones)):
prefix_sums[index] = prefix_sums[index - 1] + stones[index]
# If the game must end at the last element, the score difference is its prefix sum
best_difference = prefix_sums[-1]
# Work backwards to find the optimal decision at each step
for index in range(len(stones) - 2, 0, -1):
best_difference = max(best_difference, prefix_sums[index] - best_difference)
return best_difference
Every legal move takes a prefix of the remaining stones, adds that prefix sum to the current player's score, and leaves the opponent to play from there. The best score difference is the prefix sum of a move minus the best difference the opponent can force afterward. One backward pass finds that value.
Here's how the algorithm would work step-by-step:
def stone_game_viii(stones):
prefix_sums = []
running_sum = 0
for stone in stones:
running_sum += stone
prefix_sums.append(running_sum)
best_difference = prefix_sums[-1]
for index in range(len(stones) - 2, 0, -1):
best_difference = max(best_difference, prefix_sums[index] - best_difference)
return best_difference
| Case | How to Handle |
|---|---|
| Input array with exactly two elements | The game must stop when one stone remains, so the only valid move is taking both stones immediately, resulting in the sum of the whole array. |
| Array containing entirely negative numbers | Prefix sums will become increasingly negative, but the dynamic programming state transitions will still correctly maximize the score difference. |
| Very large arrays approaching the maximum length constraints | An O(N) suffix maximum approach using prefix sums ensures the solution scales efficiently without exceeding time or memory limits. |
| Extreme values of stone weights causing integer overflow | Using 64-bit integer types for the prefix sums and score calculations prevents arithmetic overflow during summation. |
| Input array with all zeros | Prefix sums remain zero throughout, and optimal play will result in a final score difference of zero. |
| Alternating large positive and negative numbers | The suffix maximum logic correctly evaluates whether to make a move now or skip ahead to a more profitable future state. |