landscapemi的博客

# leetcode:数学： Super Ugly Number（313）

Write a program to find the nth super ugly number.

Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.

Note:
(1) 1 is a super ugly number for any given primes.
(2) The given numbers in primes are in ascending order.
(3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.

class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
vector<int> res(1, 1), idx(primes.size(), 0);
while (res.size() < n) {
vector<int> tmp;
int mn = INT_MAX;
for (int i = 0; i < primes.size(); ++i) {
tmp.push_back(res[idx[i]] * primes[i]);
}
for (int i = 0; i < primes.size(); ++i) {
mn = min(mn, tmp[i]);
}
for (int i = 0; i < primes.size(); ++i) {
if (mn == tmp[i]) ++idx[i];
}
res.push_back(mn);
}
return res.back();
}
};

#### [LeetCode 313] Super Ugly Number

2015-12-28 11:18:08

#### 【LEETCODE】313-Super Ugly Number [Python]

2016-01-26 16:32:03

#### LeetCode-313. Super Ugly Number (JAVA)超级丑数

2017-04-29 23:51:58

#### leetcode -- Super Ugly Number -- 跟ugly number系列一起复习

2015-12-22 12:01:11

#### ［LeetCode］Super Ugly Number

2015-12-04 17:33:42

#### leetcode Super Ugly Number

2015-12-03 13:06:41

#### leetcode 263: Ugly Number

2015-08-19 20:17:44

#### [leetcode] 264. Ugly Number II 解题报告

2015-12-13 15:54:50

#### Ugly Number（丑数）系列题 Python解法

2016-05-08 14:27:16