Python3 l Faster than 99.35 % | using heapq
Instead of checking the numbers one by one, here we are generating the numbers from the already available ugly numbers and keeping a count.\nFor example: primes
Instead of checking the numbers one by one, here we are generating the numbers from the already available ugly numbers and keeping a count.\nFor example: primes = [2,3,5] and n = 16\nHere catch is there are multiple additions to heap like 6 = 2\\*3 and also 6 = 3\\*2\nso what we do is :\nfor 2, we find multiples of 2 like 2\\*2 = 4 \nfor 3, we find multiples of 2 and 3 like 3\\*2 , 3\\*3 = 6,9\nfor 5, we find multiples of 2, 3 and 5 like 5\\*2, 5\\*3, 5\\*5  = 10,15,25\n```\nclass Solution:\n    def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:\n        nums = primes.copy() # do a deep copy \n        heapq.heapify(nums) #create a heap\n        p = 1\n        for i in range(n - 1):\n            p = heapq.heappop(nums) #take the smallest element\n            for prime in primes:\n                heapq.heappush(nums, p * prime) #add all those multiples with the smallest number\n                if p % prime == 0:\n\t\t\t\t\tbreak\n        return p\n```
['Heap (Priority Queue)', 'Python', 'Python3']