File: divisor-game/solution.py

Date: 2026-06-06

Time: 16:27

divisor-game/solution.py

Purpose

This file implements the solution to LeetCode 1025 - Divisor Game. It belongs to a large collection of LeetCode solutions, each in its own directory with a standard layout (solution.py, test_solution.py, plan.md, review.md).

The problem: Alice and Bob take turns. On each turn, the current player picks a divisor x of the current number n where 0 < x < n, then replaces n with n - x. The player who cannot move (when n == 1) loses. Alice goes first. Return whether Alice wins with optimal play.

Key Components

Solution.divisorGame(self, n: int) -> bool — The core method. It exploits the mathematical insight that Alice wins if and only if n is even. The entire game-theoretic analysis collapses to a parity check.

mincostTickets = divisorGame — A class-level alias that binds mincostTickets to the same method object. This is an artifact of the repo's test harness or code generation — it has nothing to do with LeetCode 983 (Minimum Cost For Tickets). The alias exists so that test files from *other* problems can import and instantiate Solution from this module without failing on attribute lookup. The "Imported By" list (300+ test files) confirms this: the alias makes this module a drop-in when another problem's test infrastructure resolves Solution generically.

Patterns

Dependencies

Imports: None. The solution is self-contained — no stdlib, no third-party packages.

Imported by: Hundreds of test files across the repository. This is not because those problems depend on the divisor game logic — it's because the test harness resolves Solution generically and this module satisfies the interface. The actual test for *this* problem is divisor-game/test_solution.py.

Flow

1. Caller invokes divisorGame(n).

2. n % 2 == 0 is evaluated — a single modulo and comparison.

3. Returns True (Alice wins) or False (Bob wins).

No loops, no recursion, no mutation. O(1) time and space.

Invariants

Error Handling

None. The function is a pure expression with no failure modes for valid input. For n == 0, n % 2 == 0 returns True, which would be semantically wrong for the game but outside the stated constraint.

Topics to Explore

Beliefs