返回介绍

solution / 2500-2599 / 2549.Count Distinct Numbers on Board / README_EN

发布于 2024-06-17 01:03:04 字数 3254 浏览 0 评论 0 收藏 0

2549. Count Distinct Numbers on Board

中文文档

Description

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. 

 

Constraints:

  • 1 <= n <= 100

Solutions

Solution 1: Lateral Thinking

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.

Note that $n$ could be $1$, so it needs to be specially judged.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def distinctIntegers(self, n: int) -> int:
    return max(1, n - 1)
class Solution {
  public int distinctIntegers(int n) {
    return Math.max(1, n - 1);
  }
}
class Solution {
public:
  int distinctIntegers(int n) {
    return max(1, n - 1);
  }
};
func distinctIntegers(n int) int {
  return max(1, n-1)
}
function distinctIntegers(n: number): number {
  return Math.max(1, n - 1);
}
impl Solution {
  pub fn distinct_integers(n: i32) -> i32 {
    (1).max(n - 1)
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文