[Python3] k pointers
An interesting fact about ugly number or super ugly number is that they are self-generating. It is possible to write a function to generate next ugly number (e.
An interesting fact about ugly number or super ugly number is that they are self-generating. It is possible to write a function to generate next ugly number (e.g. this [post](https://leetcode.com/problems/ugly-number-ii/discuss/720034/Python3-7-line-dp)). Unfortunately, the scale of this problem is so big that a direct top-down approach causes TLE. \n\n\n```\nclass Solution:\n    def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:\n        ans = [1]\n        ptr = [0]*len(primes) #all pointing to 0th index\n        for _ in range(1, n): \n            ans.append(min(ans[ptr[i]]*p for i, p in enumerate(primes)))\n            for i, p in enumerate(primes):\n                if ans[ptr[i]] * p == ans[-1]: ptr[i] += 1\n        return ans[-1]\n```\n\nfaster via a heap (280ms, 95.49%)\n```\nclass Solution:\n    def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:\n        ans = [1]\n        hp = [(p, 0) for p in primes]\n        for _ in range(1, n): \n            ans.append(hp[0][0])\n            while ans[-1] == hp[0][0]: \n                val, i = heappop(hp)\n                val = val//(ans[i]) * ans[i+1]\n                heappush(hp, (val, i+1))\n        return ans[-1]\n```
['Python3']