Divisor Game — LeetCode 1025 Python Solution

EasyBrainteaserMathDynamic ProgrammingGame Theory
Problem
#1025
Reading time
2 min

The problem

Alice and Bob take turns playing a game, with Alice starting first. Initially, there is a number n on the chalkboard.

Example

Input
n = 2
Output
true
Explanation
Alice chooses 1, and Bob has no more moves.

Python solution

Python
class Solution:
    def divisorGame(self, n: int) -> bool:
        return n % 2 == 0

Complexity

MeasureComplexity
TimeO(1)
SpaceO(1) auxiliary

Pattern: Dynamic Programming

Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 1025. Divisor Game is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.

The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1025. Divisor Game?
LeetCode 1025. Divisor Game is rated Easy on LeetCode.
What is the time complexity of LeetCode 1025. Divisor Game?
The Python solution on this page runs in O(1).
What is the space complexity of LeetCode 1025. Divisor Game?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1025. Divisor Game cover?
LeetCode 1025. Divisor Game is tagged Brainteaser, Math, Dynamic Programming and Game Theory on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview