Stone Game IX Problem
Description
LeetCode Problem 2029.
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 i^th 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.
Example 1:
Input: stones = [2,1]
Output: true
Explanation: The game will be played as follows:
- Turn 1: Alice can remove either stone.
- Turn 2: Bob removes the remaining stone.
The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob loses and Alice wins the game.
Example 2:
Input: stones = [2]
Output: false
Explanation: Alice will remove the only stone, and the sum of the values on the removed stones is 2.
Since all the stones are removed and the sum of values is not divisible by 3, Bob wins the game.
Example 3:
Input: stones = [5,1,2,4,3]
Output: false
Explanation: Bob will always win. One possible way for Bob to win is shown below:
- Turn 1: Alice can remove the second stone with value 1. Sum of removed stones = 1.
- Turn 2: Bob removes the fifth stone with value 3. Sum of removed stones = 1 + 3 = 4.
- Turn 3: Alices removes the fourth stone with value 4. Sum of removed stones = 1 + 3 + 4 = 8.
- Turn 4: Bob removes the third stone with value 2. Sum of removed stones = 1 + 3 + 4 + 2 = 10.
- Turn 5: Alice removes the first stone with value 5. Sum of removed stones = 1 + 3 + 4 + 2 + 5 = 15.
Alice loses the game because the sum of the removed stones (15) is divisible by 3. Bob wins the game.
Constraints:
1 <= stones.length <= 10^51 <= stones[i] <= 10^4
Solution
Approach: Greedy + Case Analysis
Only each stone's value modulo 3 matters, so count the stones with remainder 0, 1 and 2 in cnt[0], cnt[1] and cnt[2]. A player loses when they are forced to make the running sum divisible by 3 (or when all stones are gone and it is Bob's turn).
Alice's first stone cannot have remainder 0, so she starts with a 1 or a 2. Suppose she starts with a 1; the sum is now 1 (mod 3). A 2 would make it 0, so Bob must take a 1 (sum 2); then a 1 would make it 0, so Alice must take a 2 (sum 1), and so on: the forced order is 1, 1, 2, 1, 2, ... Stones with remainder 0 never change the sum, they just pass the turn, so they can be played at any time. The game therefore lasts 1 + 2 * min(cnt[1], cnt[2]) + cnt[0] moves after Alice's first stone is taken from cnt[1], plus one more move if a 1 is left over (cnt[1] > cnt[2]). Alice wins when that number of moves is odd, because then Bob is the one who has to move next, and he loses if he is forced into a multiple of 3, which happens when stones are still left (cnt[1] != cnt[2] at the end). If the stones run out instead, Bob wins.
Starting with a 2 is the same with the roles of remainders 1 and 2 swapped. Alice wins if either start works.
Complexity: O(n) time and O(1) extra space.
class Solution:
def stoneGameIX(self, stones: List[int]) -> bool:
def check(cnt: List[int]) -> bool:
if cnt[1] == 0:
return False
cnt[1] -= 1
r = 1 + min(cnt[1], cnt[2]) * 2 + cnt[0]
if cnt[1] > cnt[2]:
cnt[1] -= 1
r += 1
return r % 2 == 1 and cnt[1] != cnt[2]
c1 = [0] * 3
for x in stones:
c1[x % 3] += 1
c2 = [c1[0], c1[2], c1[1]]
return check(c1) or check(c2)