Skip to content

Commit 7e439fe

Browse files
committed
超级丑数
1 parent a11f1df commit 7e439fe

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

super_ugly_number.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)