Game of Nim Problem
Description
LeetCode Problem 1908.
Alice and Bob take turns playing a game with Alice starting first.
In this game, there are n piles of stones. On each player's turn, the player should remove any positive number of stones from a non-empty pile of his or her choice. The first player who cannot make a move loses, and the other player wins.
Given an integer array piles, where piles[i] is the number of stones in the i^th pile, return true if Alice wins, or false if Bob wins.
Both Alice and Bob play optimally.
Example 1:
Input: piles = [1]
Output: true
Explanation: There is only one possible scenario:
- On the first turn, Alice removes one stone from the first pile. piles = [0].
- On the second turn, there are no stones left for Bob to remove. Alice wins.
Example 2:
Input: piles = [1,1]
Output: false
Explanation: It can be proven that Bob will always win. One possible scenario is:
- On the first turn, Alice removes one stone from the first pile. piles = [0,1].
- On the second turn, Bob removes one stone from the second pile. piles = [0,0].
- On the third turn, there are no stones left for Alice to remove. Bob wins.
Example 3:
Input: piles = [1,2,3]
Output: false
Explanation: It can be proven that Bob will always win. One possible scenario is:
- On the first turn, Alice removes three stones from the third pile. piles = [1,2,0].
- On the second turn, Bob removes one stone from the second pile. piles = [1,1,0].
- On the third turn, Alice removes one stone from the first pile. piles = [0,1,0].
- On the fourth turn, Bob removes one stone from the second pile. piles = [0,0,0].
- On the fifth turn, there are no stones left for Alice to remove. Bob wins.
Constraints:
n == piles.length1 <= n <= 71 <= piles[i] <= 7
Follow-up: Could you find a linear time solution? Although the linear time solution may be beyond the scope of an interview, it could be interesting to know.
Solution
Approach: Nim Sum
This is the classic game of Nim, where the player who cannot move loses. By Bouton's theorem, the player to move wins exactly when the XOR of all pile sizes (the nim sum) is non-zero.
Why: from a position with non-zero XOR there is always a move that makes the XOR zero, and from a position with XOR zero every move makes it non-zero. The terminal position (all piles empty) has XOR zero and the player to move loses, so the player who keeps the XOR at zero wins.
So Alice, moving first, wins if and only if the XOR of piles is not zero.
Complexity: O(n) time and O(1) space.
class Solution:
def nimGame(self, piles: List[int]) -> bool:
x = 0
for p in piles:
x ^= p
return x != 0