1455. Uneven Split Duel
At a pottery workshop, two apprentices share several clay lumps whose weights are listed in piles. They alternate turns, and the first apprentice moves first. A turn consists of choosing one lump and cutting it into two lumps of positive integer weights that are different from each other, for example a lump of weight 7 can become 1 and 6, 2 and 5, or 3 and 4, but a lump of weight 6 cannot become 3 and 3. An apprentice who has no legal cut left loses. Both play perfectly. Return true if the first apprentice wins and false otherwise. Aim for roughly O(max(piles)^2 + n) time and O(max(piles)) space, using game values that are combined across lumps.
Example 1
- Input:
- piles = [1,2]
- Output:
- false
- Explanation:
No lump of weight 1 or 2 can be cut into two different positive parts, so the first player has no move and loses.
Example 2
- Input:
- piles = [3]
- Output:
- true
- Explanation:
The lump can be cut into 1 and 2, after which neither lump can be cut again, so the first player wins.
Example 3
- Input:
- piles = [4,5,6]
- Output:
- true
- Explanation:
The combined game values of the three lumps have a non-zero XOR, so the first player has a winning cut.
Constraints
1 ≤ n = piles.length ≤ 105
1 ≤ piles[i] ≤ 1000
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 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(M^2 + n)
- Space
- O(M)