Python DP Solution


  • 0
    class Solution(object):
        def nthSuperUglyNumber(self, n, primes):
            ans  = [1]
            dic = {}
            for prime in primes:
                dic[prime] = 0
            while len(ans) < n:
                minimum = 0xffffffff
                for pri in dic:
                    minimum = min(minimum, ans[dic[pri]] * pri)
                for pri in dic:
                    if ans[dic[pri]] * pri == minimum:
                        dic[pri] += 1
                ans.append(minimum)
            return ans[-1]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.