Divisor Game (LeetCode 1025): Solution and Explanation
Alice and Bob keep subtracting a proper divisor from n, and whoever cannot move loses. A small win/lose table reveals that the first player wins exactly when n is even.
Intuition
In a two-player game with perfect play, a position is winning if at least one move sends the opponent to a losing position, and losing if every move sends them to a winning one.
The only stuck position is 1, because no x satisfies 0 < x < 1. So the player facing 1 loses, and every other value can be solved from smaller ones.
Filling the table shows LOSE, WIN, LOSE, WIN... for 1, 2, 3, 4. Even numbers can always take 1 and hand over an odd number, and odd numbers only have odd divisors, so they always hand back an even number.
Approach: Bottom-up win/lose DP
- Create
dpof size n + 1, all false. dp[1] stays false. - For each
ifrom 2 to n, try everyxfrom 1 to i - 1. - If
xdividesianddp[i - x]is false, the move strands the opponent on a losing number: setdp[i] = trueand stop. - If no such move exists, dp[i] stays false.
- Return
dp[n]. Once the pattern is clear,n % 2 == 0gives the same answer.
Step-by-step example
n = 4
- dp[1] = false: there is no legal move from 1.
- i = 2: x = 1 divides 2 and leaves 1, a losing number. dp[2] = true.
- i = 3: the only legal move is x = 1, which leaves 2, a winning number for the opponent. dp[3] = false.
- i = 4: x = 1 leaves 3, a losing number. dp[4] = true.
- dp[4] is true, so Alice wins.
Code: Python, Java, C++
1class Solution:
2 def divisorGame(self, n: int) -> bool:
3 dp = [False] * (n + 1)
4 for i in range(2, n + 1):
5 for x in range(1, i):
6 if i % x == 0 and not dp[i - x]:
7 dp[i] = True
8 break
9 return dp[n]1class Solution {
2 public boolean divisorGame(int n) {
3 boolean[] dp = new boolean[n + 1];
4 for (int i = 2; i <= n; i++) {
5 for (int x = 1; x < i; x++) {
6 if (i % x == 0 && !dp[i - x]) {
7 dp[i] = true;
8 break;
9 }
10 }
11 }
12 return dp[n];
13 }
14}1class Solution {
2public:
3 bool divisorGame(int n) {
4 vector<bool> dp(n + 1, false);
5 for (int i = 2; i <= n; i++) {
6 for (int x = 1; x < i; x++) {
7 if (i % x == 0 && !dp[i - x]) {
8 dp[i] = true;
9 break;
10 }
11 }
12 }
13 return dp[n];
14 }
15};Time and space complexity
O(n^2)
Each of the n positions tries up to n moves. With n at most 1000 that is fast; the parity shortcut is O(1).
O(n)
The dp table has one entry per number from 0 to n.
Other approaches
Parity (time O(1), space O(1))
Return n % 2 == 0. Even n: take 1 and the opponent faces an odd number. Odd n: every divisor is odd, so the opponent always gets an even number. Eventually the player on odd reaches 1 and loses.
Memoized recursion (time O(n^2), space O(n))
win(k) = any(k % x == 0 and not win(k - x)). Same logic top-down; the recursion depth can reach n.
Common mistakes
- Allowing x = k. The move must satisfy 0 < x < k, otherwise the board could reach 0.
- Treating dp[k] as 'Alice wins from k'. It means the player whose turn it is wins, which flips between turns.
- Stopping the search at the first legal move instead of the first move into a losing position.
Frequently asked questions
Why does an even number always win?
Take x = 1, which always divides. The opponent now holds an odd number. Every divisor of an odd number is odd, and odd minus odd is even, so you get an even number back every turn. You are never stuck, because you never face 1.
Is the DP needed if parity works?
Not for the answer. The table is how you discover and convince yourself of the parity rule, and the same win/lose method solves games with no clean pattern.
What does playing optimally mean here?
Each player picks a move that leads to a losing position for the other whenever one exists. The dp recurrence encodes exactly that choice.