#3360
Stone Removal Game
EasyMathSimulationGame TheoryModulo Arithmetic
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(1) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(1)Space O(1)
Instead of simulating every possible move, we can derive a pattern based on the number of stones left. The game can be simplified to checking if the number of stones remaining after Alice's first move is a multiple of 11.
⚙️
Algorithm
4 steps- 1Step 1: If n < 10, return false (Alice cannot make a valid move).
- 2Step 2: Calculate the remaining stones after Alice's first move: remaining = n - 10.
- 3Step 3: If remaining is less than 0, return true (Alice wins).
- 4Step 4: Check if remaining % 11 == 0. If true, Alice loses; otherwise, she wins.
solution.py2 lines
1def canAliceWin(n):
2 return n >= 10 and (n - 10) % 11 != 0ℹ
Complexity note: The time complexity is O(1) because we are performing a constant number of operations regardless of the input size.
- 1Alice's winning strategy can be derived from the number of stones left after her first move.
- 2The game can be simplified using modulo arithmetic to determine winning and losing positions.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.