题目:

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. USB硬件远程共享解决iphone已停用

    悲剧的在iphone拆过电池之后,再开机显示iphone已停用,请在23000000分钟后再试一次 算算这得45年了,可以留给孙子用了... 网上除了刷机和有同步过的电脑貌似没有别的办法了 因是旧系统 ...

  2. Java BigDecimal大数字操作

    在java中提供了大数字的操作类,即java.math.BinInteger类和java.math.BigDecimal类.这两个类用于高精度计算,其中BigInteger类是针对大整数的处理类,而B ...

  3. 典型用户 persona

    persona 典型用户 1.姓名:王涛 2.年龄:22 3.收入:基本无收入 4.代表用户在市场上的比例和重要性:王涛为铁道学生.本软件的用户主要是学生和老师,尤其是广大的铁大学子,所以此典型用户的 ...

  4. solr简易安装配置

    之前弄了段时间的lucene,昨天下午开始学solr,准备用到项目中,在网上找了一些教程,有的不是讲得太复杂,就是讲得不在点上,花了不少冤枉时间.有的一上来就花过半的篇幅大讲特讲“3H”,(what, ...

  5. 23、获取app所占据的内存

    public static void getRunningAppProcessInfo(ActivityManager mActivityManager) { //ActivityManager mA ...

  6. JS学习笔记-1--基本知识和注意事项

    1.JS开始的目的主要是验证表单的输入验证 2.是一种具有面向对象能力的.解释型语言.是基于事件驱动的相对较安全的客户端脚本语言 3.JS 特点:松散型:变量不具备一个明确的类型:   对象属性:把属 ...

  7. hdu 4003 Find Metal Mineral 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...

  8. Sqli-labs less 26a

    Less-26a 这关与26的区别在于,sql语句添加了一个括号,同时在sql语句执行抛出错误后并不在前台页面输出.所有我们排除报错注入,这里依旧是利用union注入. sql语句为SELECT * ...

  9. Scala学习——数组/映射/元组

    [<快学Scala>笔记] 数组 / 映射 / 元组 一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初 ...

  10. 【二】php常用方法

    -------------------------------数据类型------------------------------------------ 1.settype(var,type)  类 ...