[LeetCode/Python] 1492. The kth Factor of n

2024. 7. 23. 01:42·Algorithm/LeetCode

n의 약수를 찾고 그 중 k번째 숫자를 리턴하는 문제이다.

간단한 문제였다.

 

class Solution:
    def kthFactor(self, n: int, k: int) -> int:
        divisor = set([])

        for i in range(1, int(n**(1/2)) + 1):
            if (n % i == 0):
                divisor.add(i)
                divisor.add(n // i)
        
        if k > len(divisor):
            return -1

        return sorted(list(divisor))[k-1]

 

시간 복잡도

약수를 구할 숫자: N

=> O(1/2N)

 

 

다른 사람 풀이

- 전체 배열을 구하지 않고 딱 k번째 일때 리턴하는 풀이... 와우... 그러게 쓸데없이 다 구할 필요는 없었다!

class Solution(object):
    def kthFactor(self, n, k):
        count = 0
        for i in range(1,n+1):
            if n % i == 0 :
                count+=1
                if count == k: return i
        return -1

 

'Algorithm > LeetCode' 카테고리의 다른 글

[LeetCode/Python] 125. Valid Palindrome  (0) 2025.04.01
[LeetCode/Python] 58. Length of Last Word  (0) 2025.03.28
[LeetCode/Python] 9. Palindrome Number  (0) 2024.09.17
[LeetCode/Python] 67. Add Binary  (0) 2024.09.16
[LeetCode/Python] 13. Roman to Integer  (0) 2024.08.12
'Algorithm/LeetCode' 카테고리의 다른 글
  • [LeetCode/Python] 58. Length of Last Word
  • [LeetCode/Python] 9. Palindrome Number
  • [LeetCode/Python] 67. Add Binary
  • [LeetCode/Python] 13. Roman to Integer
빵빵0
빵빵0
(아직은) 공부하고 정리하는 블로그입니다.
  • 빵빵0
    Hack Your World
    빵빵0
  • 전체
    오늘
    어제
    • 분류 전체보기 (92)
      • Error Handling (7)
      • Project (5)
        • MEV (2)
      • Architecture (0)
        • API (0)
        • Cache (0)
        • 사소한 고민거리 (0)
      • Computer Science (4)
        • Data Structure (2)
        • Database (1)
        • Cloud (0)
        • OS (0)
        • Infra, Network (1)
        • AI (0)
      • Language (8)
        • Go (8)
        • Rust (0)
        • Python (0)
        • Java (0)
      • Algorithm (40)
        • BaekJoon (18)
        • Programmers (7)
        • LeetCode (6)
        • NeetCode (9)
      • SW Books (9)
        • gRPC Up & Running (1)
        • System Design Interview (2)
        • 스프링 입문을 위한 자바 객체지향의 원리와 이해 (6)
        • 블록체인 해설서 (0)
        • 후니의 쉽게 쓴 CISCO 네트워킹 (0)
      • BlockChain (4)
        • Issues (0)
        • Research (4)
        • Tech (0)
      • Own (8)
        • TIR(Today I Read) (3)
        • Personal (2)
        • Novel (0)
        • Memo (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    BaekJoon
    chart
    blockchain
    context
    EVM
    DP
    go
    MongoDB
    BFS
    블록체인
    2024
    candlechart
    BEAKJOON
    LeetCode
    KBW
    스택
    프로그래머스
    MEV
    백준
    Hash Table
    two pointer
    Palindrome
    Greedy
    Python
    golang
    NeetCode
    큐
    ethereum
    goroutine
    Programmers
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
빵빵0
[LeetCode/Python] 1492. The kth Factor of n
상단으로

티스토리툴바