Skip to content

Latest commit

 

History

History
28 lines (19 loc) · 422 Bytes

264.md

File metadata and controls

28 lines (19 loc) · 422 Bytes

Ugly Number II

Description

link


Solution

  • See Code

Code

Complexity T : O(nlogn) M : O(n)

class Solution:
    def nthUglyNumber(self, n: int) -> int:
    	N, m, S = [1], 1, set()
    	for _ in range(n):
    		while m in S: m = heapq.heappop(N)
    		S.add(m)
    		for i in [2,3,5]: heapq.heappush(N,i*m)
    	return m