Description
class Solution {
bool canWin(vector<int>& count, int sum, int turn) {
if(count[0] + count[1] + count[2] == 0) {
if(turn == 1) return true;
return false;
}
if(count[0] > 0) {
--count[0];
return !canWin(count, sum, 1 - turn);
} else if(sum % 3 == 1) {
if(count[1] > 0) {
--count[1];
return !canWin(count, sum + 1, 1 - turn);
}
return false;
} else {
if(count[2] > 0) {
--count[2];
return !canWin(count, sum + 2, 1 - turn);
}
return false;
}
}
public:
bool stoneGameIX(vector<int>& stones) {
vector<int> count(3, 0);
for(int stone: stones) {
++count[stone % 3];
}
auto tmp = count;
// 0 for Alice, 1 for Bob, next turn should be Bob
if(count[1] > 0) {
--tmp[1];
if(!canWin(tmp, 1, 1)) return true;
}
tmp = count;
if(count[2] > 0) {
--tmp[2];
if(!canWin(tmp, 2, 1)) return true;
}
return false;
}
};