#1025
Divisor Game
EasyMathDynamic ProgrammingBrainteaserGame TheoryGame TheoryDynamic Programming
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)
The optimal solution leverages the observation that if n is even, Alice can always make it odd for Bob, leading to a win. Conversely, if n is odd, Bob can always return it to even for Alice, leading to his win. Thus, the outcome depends solely on the parity of n.
⚙️
Algorithm
3 steps- 1Step 1: Check if n is even.
- 2Step 2: If n is even, return true (Alice wins).
- 3Step 3: If n is odd, return false (Bob wins).
solution.py2 lines
1def divisorGame(n):
2 return n % 2 == 0ℹ
Complexity note: The time complexity is O(1) because we only perform a single modulus operation, and space complexity is also O(1) as we use no additional space.
- 1Alice wins if n is even; Bob wins if n is odd.
- 2The game can be reduced to a simple parity check.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.