|
| 1 | +--- |
| 2 | +id: count-distinct-numbers-on-board |
| 3 | +title: Count Distinct Numbers on Board |
| 4 | +sidebar_label: 2544 Count Distinct Numbers on Board |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Hash Table |
| 8 | + - LeetCode |
| 9 | + - C++ |
| 10 | +description: "This is a solution to the Count Distinct Numbers on Board problem on LeetCode." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +You are given a positive integer n, that is initially placed on a board. Every day, for 109 days, you perform the following procedure: |
| 16 | + |
| 17 | +For each number x present on the board, find all numbers `1 <= i <= n` such that x % i == 1. |
| 18 | +Then, place those numbers on the board. |
| 19 | +Return the number of distinct integers present on the board after 109 days have elapsed. |
| 20 | + |
| 21 | +### Examples |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | + |
| 25 | +``` |
| 26 | +
|
| 27 | +Input: n = 5 |
| 28 | +Output: 4 |
| 29 | +Explanation: Initially, 5 is present on the board. |
| 30 | +
|
| 31 | +``` |
| 32 | + |
| 33 | +**Example 2:** |
| 34 | + |
| 35 | +``` |
| 36 | +Input: n = 3 |
| 37 | +Output: 2 |
| 38 | +Explanation: |
| 39 | +Since 3 % 2 == 1, 2 will be added to the board. |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +### Constraints |
| 44 | + |
| 45 | +- `1 <= n <= 100` |
| 46 | + |
| 47 | + |
| 48 | +### Approach |
| 49 | + |
| 50 | +Since every operation on the number n on the desktop will also cause the number n-1 to appear on the desktop, the final numbers on the desktop are [2,...n], that is, n-1 numbers. |
| 51 | + |
| 52 | +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the given number. |
| 53 | + |
| 54 | +#### Python3 |
| 55 | + |
| 56 | +```python |
| 57 | +class Solution: |
| 58 | + def distinctIntegers(self, n: int) -> int: |
| 59 | + return max(1, n - 1) |
| 60 | +``` |
| 61 | + |
| 62 | +#### Java |
| 63 | + |
| 64 | +```java |
| 65 | +class Solution { |
| 66 | + public int distinctIntegers(int n) { |
| 67 | + return Math.max(1, n - 1); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +#### C++ |
| 73 | + |
| 74 | +```cpp |
| 75 | +class Solution { |
| 76 | +public: |
| 77 | + int distinctIntegers(int n) { |
| 78 | + return max(1, n - 1); |
| 79 | + } |
| 80 | +}; |
| 81 | +``` |
| 82 | +
|
| 83 | +#### Go |
| 84 | +
|
| 85 | +```go |
| 86 | +func distinctIntegers(n int) int { |
| 87 | + return max(1, n-1) |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +#### TypeScript |
| 92 | + |
| 93 | +```ts |
| 94 | +function distinctIntegers(n: number): number { |
| 95 | + return Math.max(1, n - 1); |
| 96 | +} |
| 97 | +``` |
0 commit comments