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.

Notice:
1 is a super ugly number for any given primes.
The given numbers in primes are in ascending order.
0 < k ≤ 100, 0 < n ≤ 10^6, 0 < primes[i] < 1000

Example
Given n = 6, primes = [2, 7, 13, 19] return 13

LeetCode上的原题,请参见我之前的博客Super Ugly Number

解法一:

class Solution {
public:
/**
* @param n a positive integer
* @param primes the given prime list
* @return the nth super ugly number
*/
int nthSuperUglyNumber(int n, vector<int>& primes) {
vector<int> res(, ), pos(primes.size(), );
while (res.size() < n) {
vector<int> t;
for (int i = ; i < primes.size(); ++i) {
t.push_back(res[pos[i]] * primes[i]);
}
int mn = INT_MAX;
for (int i = ; i < primes.size(); ++i) {
mn = min(mn, t[i]);
}
for (int i = ; i < primes.size(); ++i) {
if (t[i] == mn) ++pos[i];
}
res.push_back(mn);
}
return res.back();
}
};

解法二:

class Solution {
public:
/**
* @param n a positive integer
* @param primes the given prime list
* @return the nth super ugly number
*/
int nthSuperUglyNumber(int n, vector<int>& primes) {
vector<int> dp(n, ), pos(primes.size(), );
for (int i = ; i < n; ++i) {
dp[i] = INT_MAX;
for (int j = ; j < primes.size(); ++j) {
dp[i] = min(dp[i], dp[pos[j]] * primes[j]);
}
for (int j = ; j < primes.size(); ++j) {
if (dp[i] == dp[pos[j]] * primes[j]) {
++pos[j];
}
}
}
return dp.back();
}
};

[LintCode] Super Ugly Number 超级丑陋数的更多相关文章

  1. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  2. [LeetCode] 313. Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  3. 313 Super Ugly Number 超级丑数

    编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...

  4. [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了

    丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...

  5. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  6. [LeetCode] Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  7. [LeetCode] 264. Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  8. [Swift]LeetCode313. 超级丑数 | Super Ugly Number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  9. 313. Super Ugly Number

    题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...

随机推荐

  1. JVM内存结构、垃圾回收那点事

    翻看电脑的文件夹,无意看到了9月份在公司做的一次分享,浏览了一下"婆婆特",发现自己在ppt上的写的引导性问题自己也不能确切的回答出来,哎,知识这东西,平时不常用的没些日子就生疏了 ...

  2. OpenMesh 读写网格控制(读取写入纹理坐标,法向等)

    OpenMesh读取网格默认是不自动读取obj网格中的法向,纹理坐标等信息的,写入网格同样也是.所以要读取(或写入)这些信息需要修改默认的选项. 先看一下其读写网格的函数 template<cl ...

  3. 标签q

    标记短的引用,默认是中文符号:双引号 <p>文字<q>段落中的引用</q>文字</p> 如果是在html里直接敲出引号,是这样的: <p>文 ...

  4. JAVA Day2

          标识符(类名:变量.属性.方法名: )      组成:类名开头不能是数字,只能有字母数字_$组成.         命名规范: 类名每一个单词首字母大写(HelloWorld大驼峰法则) ...

  5. java发送短信--httpclient方式

    最近头让我写个发送短信的java程序检测BI系统,检查数据库是否有异常发送,有则发送短信到头的手机里.这里我直说httpclient方式的get请求方式,并且已经有方式的短信的接口了,所以只要再加上参 ...

  6. Codeforces Round #347 (Div. 2)

    unrating的一场CF A - Complicated GCD #include <bits/stdc++.h> const int N = 1e5 + 5; char a[105], ...

  7. Hive 实现HBase 数据批量插入

    HBase 数据的插入可以使用Java API 来写Java 程序逐条倒入,但是不是很方便.利用Hive自带的一个Jar包,可以建立Hive和HBase的映射关系 利用Hive 的insert可以将批 ...

  8. 疯狂java学习笔记之面向对象(三) - 方法所属性和值传递

    方法的所属性: 从语法的角度来看:方法必须定义在类中 方法要么属于类本身(static修饰),要么属于实例 -- 到底是属于类还是属于对象? 有无static修饰 调用方法时:必须有主调对象(主语,调 ...

  9. How to retrieve instance parameters from an uninstantiated (uninserted) family

    The trick to be able to read the default values for instance parameters is to get to the FamilyManag ...

  10. 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest F. Friends

    F. Friends time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...