Участник:Vovaf709/ugly number

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/ugly-number-ii/

class Solution:
    def nthUglyNumber(self, n: int) -> int:
        nth, h = 0, [1]
        heapq.heapify(h)
        while n:
            if nth == (x := heapq.heappop(h)): 
                continue
            for p in [2, 3, 5]: 
                heapq.heappush(h, p*x)
            nth = x
            n -= 1
        return nth