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.
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.Clarification:
1 <= n <= 100-
說明
- 一開始排除等於 1 的值
- 在 forloop 避開 i,j = 0 的情形, 因為 0 % 1 會噴錯
- 最後若 i % j == 1 則寫入 set 中
- return set 數量
-
時間複雜度:$O(n^2)$
-
空間複雜度:$O(n)$
class Solution { public int distinctIntegers(int n) { Set<Integer> storeSet = new HashSet<>(); if (n == 1) { return 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i % j == 1) { storeSet.add(j); } } } return storeSet.size(); } }
一種優化方法是,觀察到當
這個解法首先遍歷每個
- 運算過程
以下是一个 n = 5 的例子:
对于 x = 1,需要找到所有满足 x % i == 1 且 i > 1 的 i 值:
当 i = 2 时,1 % 2 = 1,符合条件,需要在棋盘上加上数字 2;
当 i = 3 时,1 % 3 = 1,符合条件,需要在棋盘上加上数字 3;
当 i = 4 时,1 % 4 = 1,符合条件,需要在棋盘上加上数字 4;
当 i = 5 时,1 % 5 = 1,符合条件,需要在棋盘上加上数字 5。
对于 x = 2,需要找到所有满足 x % i == 1 且 i > 1 的 i 值:
当 i = 3 时,2 % 3 = 2,符合条件,需要在棋盘上加上数字 3;
当 i = 4 时,2 % 4 = 2,符合条件,需要在棋盘上加上数字 4;
当 i = 5 时,2 % 5 = 2,符合条件,需要在棋盘上加上数字 5。
对于 x = 3,需要找到所有满足 x % i == 1 且 i > 1 的 i 值:
当 i = 4 时,3 % 4 = 3,符合条件,需要在棋盘上加上数字 4;
当 i = 5 时,3 % 5 = 3,符合条件,需要在棋盘上加上数字 5。
对于 x = 4,需要找到所有满足 x % i == 1 且 i > 1 的 i 值:
当 i = 5 时,4 % 5 = 4,符合条件,需要在棋盘上加上数字 5。
对于 x = 5,没有符合条件的 i 值,不需要在棋盘上加数字。
最终,棋盘上出现的数字为 2、3、4、5,一共 4 个数字,因此返回 4。-
時間複雜度:$O(n\log n)$
-
空間複雜度:$O(n)$
class Solution { public int distinctIntegers_chatgpt(int n) { if (n == 1) { return 1; } Set<Integer> storeSet = new HashSet<>(); for (int i = 2; i <= n; i++) { int j = i - 1; while (j > 0 && (i % j) == 1) { storeSet.add(j); j = j - 1; } } return storeSet.size() + 1; } }
-
說明
-
要求找到所有滿足
x % i == 1的i,並把這些i放到棋盤上。 -
對於每個數 x,當
i=1時,x % i恆為 0,所以不需要考慮i=1的情況。 -
因此,我們需要找到所有滿足
x % i == 1且i > 1的i值,這裡i > 1中的 1是棋盤上的數字。 -
所以在計算
x % i時,我們需要將i加 1。 -
例如,對於
x=2,要找到所有滿足x % i == 1且i > 1的 i 值,計算方式為:- 當
i=2時,2 % 2 = 0不符合條件; - 當
i=3時,2 % 3 = 2符合條件。 因此,我們需要在棋盤上加上數字 3。
- 當
-
-
計算過程
Day 0: 1 Day 1: 2 (1%1+1=2) Day 2: 2, 3 (2%1+1=2, 2%2+1=3) Day 3: 2, 3, 4 (2%1+1=2, 2%2+1=3, 3%3+1=4) Day 4: 2, 3, 4, 5 (2%1+1=2, 2%2+1=3, 3%3+1=4, 2%4+1=3, 5%5+1=6) Day 5: 2, 3, 4, 5, 6 (2%1+1=2, 2%2+1=3, 3%3+1=4, 2%4+1=3, 5%5+1=6, 2%6+1=3, 3%6+1=4)
class Solution {
public int distinctIntegers_chatgpt(int n) {
return n == 1 ? 1 : n - 1;
}
}- 時間複雜度:$O(1)$
- 空間複雜度:$O(1)$
- 時間複雜度:$O(nlogn)$
- 空間複雜度:$O(n)$
- 時間複雜度:$O(n)$
- 空間複雜度:$O(n)$
-
java
-
Code
class Solution { }
-