-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2549-CountDistinctNumbersOnBoard.go
More file actions
72 lines (60 loc) · 2.25 KB
/
2549-CountDistinctNumbersOnBoard.go
File metadata and controls
72 lines (60 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
// 2549. Count Distinct Numbers on Board
// You are given a positive integer n, that is initially placed on a board. Every day, for 10^9 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 10^9 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.
// Example 1:
// Input: n = 5
// Output: 4
// Explanation: Initially, 5 is present on the board.
// The next day, 2 and 4 will be added since 5 % 2 == 1 and 5 % 4 == 1.
// After that day, 3 will be added to the board because 4 % 3 == 1.
// At the end of a billion days, the distinct numbers on the board will be 2, 3, 4, and 5.
// Example 2:
// Input: n = 3
// Output: 2
// Explanation:
// Since 3 % 2 == 1, 2 will be added to the board.
// After a billion days, the only two distinct numbers on the board are 2 and 3.
// Constraints:
// 1 <= n <= 100
import "fmt"
func distinctIntegers(n int) int {
if n == 1 {
return 1
}
return n - 1
}
func distinctIntegers1(n int) int {
set := make(map[int]bool)
queue := []int{n}
for len(queue) > 0 {
x := queue[0]
if !set[x] {
set[queue[0]] = true
for i := 1; i <= n; i++ {
if x % i == 1 {
queue = append(queue, i)
}
}
}
queue = queue[1:]
}
return len(set)
}
func main() {
// Explanation: Initially, 5 is present on the board.
// The next day, 2 and 4 will be added since 5 % 2 == 1 and 5 % 4 == 1.
// After that day, 3 will be added to the board because 4 % 3 == 1.
// At the end of a billion days, the distinct numbers on the board will be 2, 3, 4, and 5.
fmt.Println(distinctIntegers(5)) // 4
// Since 3 % 2 == 1, 2 will be added to the board.
// After a billion days, the only two distinct numbers on the board are 2 and 3.
fmt.Println(distinctIntegers(3)) // 2
fmt.Println(distinctIntegers1(5)) // 4
fmt.Println(distinctIntegers1(3)) // 2
}