[LeetCode] Stone Game IX

2029. Stone Game IX

Alice and Bob continue their games with stones. There is a row of n stones, and each stone has an associated value. You are given an integer array stones, where stones[i] is the value of the ith stone.

Alice and Bob take turns, with Alice starting first. On each turn, the player may remove any stone from stones. The player who removes a stone loses if the sum of the values of all removed stones is divisible by 3. Bob will win automatically if there are no remaining stones (even if it is Alice’s turn).

Assuming both players play optimally, return true if Alice wins and false if Bob wins.

1
2
3
4
5
6
7
8
class Solution {
public:
bool stoneGameIX(vector<int>& stones) {
int cnt[3]{};
for(auto s : stones) ++cnt[s % 3];
return !min(cnt[1], cnt[2]) ? max(cnt[1], cnt[2]) > 2 and cnt[0] & 1 : abs(cnt[1] - cnt[2]) > 2 or !(cnt[0] & 1);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/12/14/PS/LeetCode/stone-game-ix/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.