313. Super Ugly Number
题目:
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的更多相关文章
- 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 ...
- Leetcode 313. super ugly number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] 313. Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 263. Ugly Number + 264. Ugly Number II + 313. Super Ugly Number
▶ 三个与丑数相关的问题 ▶ 第 263题,判定一个数字是否是丑数,即其素因子是否仅由 2,3,5 构成. ● 常规消除判别,4 ms class Solution { public: bool is ...
- [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了
丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...
- 313 Super Ugly Number 超级丑数
编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...
- 【leetcode】313. Super Ugly Number
题目如下: 解题思路:总结一下这么几点,一出一进,优先级队列排序,保证每次输出的都是当前的最小值.解法大致如图: 代码如下: #include<map> #include<queue ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LintCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
随机推荐
- MySQL 存储过程学习笔记
存储过程框架 DEMILITER $$ -- 重定义符 DROP PROCEDURE IF EXISTS store_procedure$$ -- 如果存在此名的存储过程,先删除 CREATE PRO ...
- Session invalidate
会清空所有已定义的session 而不是清空全部session的值也就是说 定义了一个名为 user 的session 调用invalidate()方法后使用Session.getValue(“use ...
- Window VNC远程控制LINUX:VNC详细配置介绍
Window VNC远程控制LINUX:VNC详细配置介绍 //---------------------------------------vnc linux下的详细配置 1.VNC的启动/停止/重 ...
- 华为p7怎么打开usb调试模式
在应用程序列表中选择[设置]进入系统设置菜单,点击[关于手机]  2.在"版本号"上面连续点击七次:  3.现在返回"设置"界面,发现多了一个"开 ...
- 如何做一个脚本自动打开IE浏览器
打开记事本,输入start iexplore "http://www.baidu.com"这个是打开百度,如果只要打开IE就输入start iexplore然后另存为--保存类型改 ...
- Careercup - Google面试题 - 6283958983589888
2014-05-06 11:31 题目链接 原题: Find the k-th Smallest Element in Two Sorted Arrays. I followed the algori ...
- 修改placeholder文字颜色
.area_ipt ::-webkit-input-placeholder { /* WebKit browsers */ color:#258aca; } .area_ipt :-moz-place ...
- WooCommerce微信支付插件免费版下载
WooCommerce微信支付插件免费版下载 2016-05-11 点击:605 免费版来了 免费版终于来了,直接下载用吧,当然免费少一些功能,只有PC扫码支付,没有微信原生支付,没有汇率,没有退款, ...
- 记codevs第一次月赛
第一次参加这种有奖励的比赛(没错,我就是为猴子而去的 一年没怎么碰代码果然手生,还是用没写多久的C++,差点全跪了 T1数学奇才琪露诺: 首先定义一个函数F(x),F(x)=x的各个数位上的数字和 然 ...
- 02.JSP的3个编译指令
本章介绍JSP的3个编译指令,在JSP中常见的编译指令有如下3个: 1.page:用于针对当前页面的指令. 2.include:用于指定包含另一个页面. ...