super-ugly-number.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:lc-all-solutions 作者: csujedihy 项目源码 文件源码
def nthSuperUglyNumber(self, n, primes):
        """
        :type n: int
        :type primes: List[int]
        :rtype: int
        """
        dp = [0] * (n + 1)
        dp[1] = 1
        heap = []
        indexes = [1] * len(primes)
        for i in xrange(0, len(primes)):
            heapq.heappush(heap, (dp[indexes[i]] * primes[i],i))

        for i in xrange(2, n + 1):
            minV = heap[0][0]
            dp[i] = minV
            while heap[0][0] == minV:
                value, xi = heapq.heappop(heap)
                indexes[xi] += 1
                heapq.heappush(heap, (dp[indexes[xi]] * primes[xi], xi))
        return dp[-1]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号