873. Square Removal Game
Two players, Mira and Joel, share a heap of n pebbles and alternate turns, with Mira moving first. On a turn the mover must take away a positive perfect square number of pebbles (1, 4, 9, 16, ...) that does not exceed the pebbles left. A player who cannot move because the heap is empty loses.
Both players play perfectly. Return true if Mira (the first player) wins the game for the starting heap size n, and false otherwise.
Example 1
- Input:
- n = 5
- Output:
- false
- Explanation:
From 5 stones the mover can leave 4 or 1; the opponent then takes all remaining stones in either case (4 and 1 are squares), so the first player loses.
Example 2
- Input:
- n = 6
- Output:
- true
- Explanation:
The first player removes 1 stone and leaves 5, which was just shown to be a losing position for whoever must move.
Constraints
- 1 ≤ n ≤ 105
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 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(n * sqrt(n))
- Space
- O(n)