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)에 가까운 풀이라 더 좋은 풀이 아시는 분 있으면 공유 부탁드려요!
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2451. Odd String Difference (0) | 2023.03.08 |
---|---|
leetcode 2264. Largest 3-Same-Digit Number in String (0) | 2023.03.07 |
leetcode 1539. Kth Missing Positive Number (0) | 2023.03.06 |
leetcode 2164. Sort Even and Odd Indices Independently (0) | 2023.03.03 |
leetcode 203. Remove Linked List Elements (0) | 2023.02.28 |
댓글