We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a11f1df commit 7e439feCopy full SHA for 7e439fe
super_ugly_number.py
@@ -0,0 +1,21 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+class Solution:
4
+ # @param {int} n a positive integer
5
+ # @param {int[]} primes the given prime list
6
+ # @return {int} the nth super ugly number
7
+ def nthSuperUglyNumber(self, n, primes):
8
+ # Write your code here
9
+ # 参考ugly_number_ii.py的实现,推广到n个数的情况。
10
+ ret = [1]
11
+ candidates = [1] * len(primes)
12
+ ids = [0] * len(primes)
13
+ _next = 1
14
+ for count in xrange(1, n):
15
+ for i in xrange(len(primes)):
16
+ if _next == candidates[i]:
17
+ candidates[i] = ret[ids[i]] * primes[i]
18
+ ids[i] += 1
19
+ _next = min(candidates)
20
+ ret.append(_next)
21
+ return ret[-1]
0 commit comments