1060. Pile Split Game
A warehouse has several crates of bolts, where piles[i] is the number of bolts in crate i. Two workers alternate turns. On a turn the mover picks one non-empty crate, splits off a batch of between 1 and k bolts (inclusive, and no more than the crate holds) and sends that batch to recycling for good.
The worker who cannot make a move because all crates are empty loses. The first worker moves first and both play optimally. Return true if the first worker can force a win. Crates may hold up to a billion bolts, so simulating the game or tabulating every crate size is far too slow; find the structure of a single crate and combine crates with a bitwise operation.
Example 1
- Input:
- piles = [5,3], k = 2
- Output:
- true
- Explanation:
With k = 2 each pile behaves like its size modulo 3, giving 2 and 0; their XOR is 2, so the first player wins.
Example 2
- Input:
- piles = [4,4,7], k = 3
- Output:
- true
- Explanation:
Modulo 4 the piles reduce to 0, 0 and 3, whose XOR is 3 so the first player wins by working on the pile of 7.
Example 3
- Input:
- piles = [6,2,4], k = 1
- Output:
- false
- Explanation:
With k = 1 only parity matters: 0, 0, 0 XOR to zero, so the second player wins.
Constraints
1 ≤ piles.length ≤ 105
1 ≤ piles[i] ≤ 109
1 ≤ k ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms
Expected complexity
- Time
- O(n)
- Space
- O(1)