[LeetCode] Stone Game VIII

1872. Stone Game VIII

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:

  1. Choose an integer x > 1, and remove the leftmost x stones from the row.
  2. Add the sum of the removed stones’ values to the player’s score.
  3. Place a new stone, whose value is equal to that sum, on the left side of 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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int stoneGameVIII(vector<int>& stones) {
partial_sum(begin(stones), end(stones), begin(stones), plus<int>());
int res(stones.back());
for(int i = stones.size() - 2; i > 0; i--) {
res = max(res, stones[i] - res);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/05/23/PS/LeetCode/stone-game-viii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.