1 import sys 2 class Solution: 3 def nthSuperUglyNumber(self, n: int, primes: 'List[int]') -> int: 4 dp = [1]*n 5 m = len(primes) 6 plist = [0] * m 7 for i in range(1, n): 8 cur = sys.maxsize 9 for j in range(m): 10 s = primes[j] * dp[plist[j]] 11 if s < cur: 12 cur = s 13 for j in range(m): 14 s = primes[j] * dp[plist[j]] 15 if cur == s: 16 plist[j] += 1 17 dp[i] = cur 18 return dp[-1]
算法思路:动态规划,本题是从 leetcode 264. Ugly Number II改造而来。