[LeetCode] Count Distinct Numbers on Board

2549. Count Distinct Numbers on Board

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.
  • Then, place those numbers on the board.

Return the number of distinct integers present on the board after 109 days have elapsed.

Note:

  • Once a number is placed on the board, it will remain on it until the end.
  • % stands for the modulo operation. For example, 14 % 3 is 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int distinctIntegers(int n) {
int res = 1;
unordered_set<int> us{n};
queue<int> q;
q.push(n);
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = 1; i < u; i++) {
if(u % i == 1 and !us.count(i)) {
res += 1;
us.insert(i);
q.push(i);
}
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/01/29/PS/LeetCode/count-distinct-numbers-on-board/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.