Count Distinct Numbers on Board — LeetCode 2549 Python Solution
- Problem
- #2549
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given a positive integer n, that is initially placed on a board. Every day, for 109 days, you perform the following procedure: For each number x present on the board, find all numbers 1 <= i <= n such that x % i == 1.
Example
- Input
- n = 5
- Output
- 4
- Explanation
- Initially, 5 is present on the board.
Python solution
class Solution:
def distinctIntegers(self, n: int) -> int:
return max(1, n - 1)Complexity
| Measure | Complexity |
|---|---|
| Time | O(1) |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 2549. Count Distinct Numbers on Board is filed here because LeetCode tags it Math, which is the vocabulary this hub collects.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2549. Count Distinct Numbers on Board?
- LeetCode 2549. Count Distinct Numbers on Board is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2549. Count Distinct Numbers on Board?
- The Python solution on this page runs in O(1).
- What is the space complexity of LeetCode 2549. Count Distinct Numbers on Board?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2549. Count Distinct Numbers on Board cover?
- LeetCode 2549. Count Distinct Numbers on Board is tagged Array, Hash Table, Math and Simulation on LeetCode.