class Solution:
def nthUglyNumber(self, n: int) -> int:
minHeap = [1]
uglySet = set()
while len(uglySet) < n:
top = heappop(minHeap)
if top in uglySet:
continue
uglySet.add(top)
heappush(minHeap, 2 * top)
heappush(minHeap, 3 * top)
heappush(minHeap, 5 * top)
uglySet = list(uglySet)
return sorted(uglySet)[n-1]
숫자가 꽤 크게 나오길래 어렵나? 했는데 상식적으로 하면 되는 문제였다.
댓글 0