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:
Choose an integer x > 1, and remove the leftmost x stones from the row.
Add the sum of the removed stones’ values to the player’s score.
Place a new stone , whose value is equal to that sum, on the left side of the row.
The game stops when onlyone 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 thescore difference between Alice and Bob if they both play optimally.
Input: stones =[-1,2,-3,4,-5]Output: 5Explanation:
- 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 is2-(-3)=5.
Input: stones =[7,-6,5,10,5,-2,-6]Output: 13Explanation:
- 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 is13-0=13.
Input: stones =[-10,-12]Output: -22Explanation:
- 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.
After the first move, the game reduces to a sequence of prefix sums. At each step, the player can choose to take the first k stones (k ≥ 2), sum them, and replace them with a single stone. The optimal strategy is to maximize the difference using DP from the end.
classSolution {
publicintstoneGameVIII(int[] stones) {
int n = stones.length;
int[] prefix =newint[n];
prefix[0]= stones[0];
for (int i = 1; i < n; i++) prefix[i]= prefix[i-1]+ stones[i];
int dp = prefix[n-1];
for (int i = n-2; i >= 1; i--) {
dp = Math.max(dp, prefix[i]- dp);
}
return dp;
}
}
1
2
3
4
5
6
7
8
9
10
11
funstoneGameVIII(stones: IntArray): Int {
val n = stones.size
val prefix = IntArray(n)
prefix[0] = stones[0]
for (i in1 until n) prefix[i] = prefix[i-1] + stones[i]
var dp = prefix[n-1]
for (i in n-2 downTo 1) {
dp = maxOf(dp, prefix[i] - dp)
}
return dp
}
1
2
3
4
5
6
7
8
9
10
defstoneGameVIII(stones: list[int]) -> int:
n = len(stones)
prefix = [0]*n
prefix[0] = stones[0]
for i in range(1, n):
prefix[i] = prefix[i-1] + stones[i]
dp = prefix[-1]
for i in range(n-2, 0, -1):
dp = max(dp, prefix[i] - dp)
return dp
1
2
3
4
5
6
7
8
9
10
11
12
13
pubfnstone_game_viii(stones: Vec<i32>) -> i32 {
let n = stones.len();
letmut prefix =vec![0; n];
prefix[0] = stones[0];
for i in1..n {
prefix[i] = prefix[i-1] + stones[i];
}
letmut dp = prefix[n-1];
for i in (1..n-1).rev() {
dp = dp.max(prefix[i] - dp);
}
dp
}