-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2029-stone-game-ix.js
More file actions
33 lines (30 loc) · 1.07 KB
/
2029-stone-game-ix.js
File metadata and controls
33 lines (30 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 2029. Stone Game IX
* https://leetcode.com/problems/stone-game-ix/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[]} stones
* @return {boolean}
*/
var stoneGameIX = function(stones) {
const remainderCount = [0, 0, 0];
for (const stone of stones) {
remainderCount[stone % 3]++;
}
if (remainderCount[0] % 2 === 0) {
return remainderCount[1] >= 1 && remainderCount[2] >= 1;
}
return Math.abs(remainderCount[1] - remainderCount[2]) > 2;
};