본문 바로가기
python-algorithm

leetcode 2549. Count Distinct Numbers on Board

by 무적김두칠 2023. 3. 7.

https://leetcode.com/problems/count-distinct-numbers-on-board/description/

 

Count Distinct Numbers on Board - LeetCode

Can you solve this real interview question? 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, f

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def distinctIntegers(self, n: int-> int:
        answer = [n]
        for first_num in range(1, n+1):
            for second_num in range(1, first_num):
                if first_num % second_num ==1:
                    answer.append(first_num)
        answer = list(set(answer))
        if n == 1 or n == 2:
            return 1
        else:
            return len(answer)+1
 
cs

거의 O(N^2)에 가까운 풀이라 더 좋은 풀이 아시는 분 있으면 공유 부탁드려요!

반응형

댓글