题目:

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.

链接: http://leetcode.com/problemset/algorithms/

题解:

超级丑数。跟丑数II很类似,只不过这次primes从2, 3, 5变成了一个size k的list。方法应该有几种,一种是维护一个size K的min-oriented heap,heap里是k个queue,和Ugly Number II的方法一样,取最小的那一个,然后更新其他Queue里的元素,n--,最后n = 1时循环结束。  另外一种是类似dynamic programming的方法,主要参考了larrywant2014大神的代码。维护一个index数组,维护一个dp数组。每次遍历更新的转移方程非常巧妙,min = dp[[index[j]]] * primes[j]。之后再便利一次primes来update每个数在index[]中的使用次数。

Time Complexity - O(nk), Space Complexity - O(n + k).

public class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
if(n < 1) {
return 0;
}
int len = primes.length;
int[] index = new int[len];
int[] dp = new int[n];
dp[0] = 1; for(int i = 1; i < n; i++) {
int min = Integer.MAX_VALUE;
for(int j = 0; j < len; j++) { // try update with all primes
min = Math.min(dp[index[j]] * primes[j], min);
}
dp[i] = min; // find dp[i]
for(int j = 0; j < len; j++) { //if prices[j] is used, increase the index
if(dp[i] % primes[j] == 0) {
index[j]++;
}
}
}
return dp[n - 1];
}
}

题外话:

休假结束,又开始上班啦。不能象前几天一样愉快地刷题了...

Reference:

https://leetcode.com/discuss/72878/7-line-consice-o-kn-c-solution

https://leetcode.com/discuss/72835/108ms-easy-to-understand-java-solution

https://leetcode.com/discuss/74433/simple-addiction-logic-reduce-the-runtime-28ms-and-beats-77%25

http://bookshadow.com/weblog/2015/12/03/leetcode-super-ugly-number/

http://www.cnblogs.com/Liok3187/p/5016076.html

http://www.hrwhisper.me/leetcode-super-ugly-number/

http://my.oschina.net/Tsybius2014/blog/547766?p=1

313. Super Ugly Number的更多相关文章

  1. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  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. [LeetCode] 313. Super Ugly Number 超级丑陋数

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

  4. 263. Ugly Number + 264. Ugly Number II + 313. Super Ugly Number

    ▶ 三个与丑数相关的问题 ▶ 第 263题,判定一个数字是否是丑数,即其素因子是否仅由 2,3,5 构成. ● 常规消除判别,4 ms class Solution { public: bool is ...

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

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

  6. 313 Super Ugly Number 超级丑数

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

  7. 【leetcode】313. Super Ugly Number

    题目如下: 解题思路:总结一下这么几点,一出一进,优先级队列排序,保证每次输出的都是当前的最小值.解法大致如图: 代码如下: #include<map> #include<queue ...

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

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

  9. [LintCode] Super Ugly Number 超级丑陋数

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

随机推荐

  1. vs2013中使用nuget下载cefsharp winform包

    cefsharp是chrome的一个开源项目,基于webkit的一个浏览器.下载cefsharp需要安装nuget.之后管理nuget程序包,联机搜索cef即可找到winform包.

  2. 模仿cocos2dx 风格用工厂方法,实现class A,不使用宏,

    class A { static A *create(); bool init(); }; A* A::create() { A *pRet=new A; if(pRet && pRe ...

  3. js 完成对图片的等比例缩放的方法

    /* 重新按比例设置 页面上对应图片的长和高, */ function resetImgSize(id,imgWidth,imgHeight,posWidth,posHeight) { var wid ...

  4. 【Add binary】cpp

    题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...

  5. 【Integer To Roman】cpp

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  6. Codeforces Round #353 (Div. 2) C Money Transfers

    题目链接: http://www.codeforces.com/contest/675/problem/C 题意: 给一个数组,每个数与他相邻的数相连,第一个与最后一个相连,每个数的数值可以左右移动, ...

  7. 【CentOS】IBM X3650M4 IMM远程管理【转载】

    问题描述:          IBM X3650M4 IMM远程开机和关机   参考资料:             http://www.ibmsys.cn/blog/?p=201   问题解决: 一 ...

  8. 网络(一),libevent客户端部分

    网络模块() 一.服务端: 暂时就以libevent模块,共享内存等下 .GS打开,首先创建4个libevent子线程,当然为每个线程设置连接通知回调函数,这个是基于sockpair的,然后再创建一个 ...

  9. javascript版Ajax请求

    什么是Ajax请求,Ajax也就是“Asynchronous JavaScript and XML”(异步JavaScript和XML),无刷新数据读取.能减少流量的消耗,也提高了浏览的流畅性,给用户 ...

  10. 威胁远胜“心脏出血”?国外新爆Bash高危安全漏洞

    这几天Linux用户们可能不能愉快地玩耍了,红帽(Redhat)安全团队昨天爆出一个危险的Bash Shell漏洞.其带来的威胁可能比早前披露的“心脏出血”漏洞更大更强! [OpenSSL心脏出血漏洞 ...