Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7.

The eligible numbers are like 3, 5, 7, 9, 15 ...

Example

If k=4, return 9.

Challenge

O(n log n) or O(n) time

Analysis:

This is the Humble number problem (丑数问题). Search the Humble Number problem online.

分析:假设数组ugly[N]中存放不断产生的丑数,初始只有一个丑数ugly[0]=1,由此出发,下一个丑数由因子2,3,5竞争产生,得到 ugly[0]*2, ugly[0]*3, ugly[0]*5, 显然最小的那个数是新的丑数,所以第2个丑数为ugly[1]=2,开始新一轮的竞争,由于上一轮竞争中,因子2获胜,这时因子2应该乘以ugly[1] 才显得公平,得到ugly[1]*2,ugly[0]*3,ugly[0]*5, 因子3获胜,ugly[2]=3,同理,下次竞争时因子3应该乘以ugly[1],即:ugly[1]*2, ugly[1]*3, ugly[0]*5, 因子5获胜,得到ugly[3]=5,重复这个过程,直到第n个丑数产生。总之:每次竞争中有一个(也可能是两个)因子胜出,下一次竞争中 胜出的因子就 应该加大惩罚!

注意这里不可以使用if/else 循环,因为有可能多于一个指针的结果是相等的:例如p3->5, p5->3, 他们的结果相等,这是两个指针都要+1

Solution:

 class Solution {
/**
* @param k: The number k.
* @return: The kth prime number as description.
*/
public long kthPrimeNumber(int k) {
if (k==0) return 1; long[] res = new long[k+1];
res[0] = 1;
int p3 = 0, p5 = 0, p7 = 0;
for (int i=1;i<=k;i++){
//find the minimum prime number.
long val = Math.min(res[p3]*3, res[p5]*5);
val = Math.min(val, res[p7]*7);
if (val / res[p3] == 3) p3++;
if (val / res[p5] == 5) p5++;
if (val / res[p7] == 7) p7++;
res[i] = val;
}
return res[k];
}
};

LintCode-Kth Prime Number.的更多相关文章

  1. Lintcode: Kth Prime Number (Original Name: Ugly Number)

    Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the kth numbe ...

  2. [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字

    Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...

  3. Lintcode: Kth Smallest Number in Sorted Matrix

    Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...

  4. 每日一九度之 题目1040:Prime Number

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  5. 问题 B: Prime Number

    题目描述 Output the k-th prime number. 输入 k≤10000 输出 The k-th prime number. 样例输入 10 50 样例输出 29 229 #incl ...

  6. 九度OJ 1040:Prime Number(质数) (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5278 解决:2180 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  7. 【九度OJ】题目1040:Prime Number 解题报告

    [九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...

  8. Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解

    [题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. 【网络收集】获取JavaScript 的时间使用内置的Date函数完成

    var mydate = new Date(); mydate.getYear(); //获取当前年份(2位) mydate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  2. 【转载】分析商品日均销量(DMS)对促销商品选择的意义

    江苏省常州市信特超市有限公司副总经理高晓颖 随着中国零售业的进一步的开放,竞争日趋激烈,促销活动在日常经营中已经成为不可缺少的一部分,频繁的促销活动的开展,零售业经营管理者越来越觉得促销商品的选择难度 ...

  3. Exchange 2013 基本部署独立与非独立

    Exchange 2013 基本部署独立与非独立 转载请注明原出处 From yang 先决条件 Active Directory需要准备的,安装Microsoft .NET Framework 4. ...

  4. win8.1开启虚拟wifi

    1. 使用管理员身份打开cmd 2. 然后输入netsh wlan set hostednetwork mode=allow 3. 接着输入netsh wlan start hostednetwork ...

  5. Professional iOS Network Programming Connecting the Enterprise to the iPhone and iPad

    Book Description Learn to develop iPhone and iPad applications for networked enterprise environments ...

  6. 【Unity3D】场景转换与退出

    1.场景转换 : 老版本的场景切换用的是Application.LoadLevel([场景名字或者在File->Build settings里面的场景代号]); 新版本的场景转换用到了Scene ...

  7. 博客转移到 海胖网 http://haipz.com/ 希望你能支持我们!

    博客转移到 海胖网 http://haipz.com/ 希望你能支持我们! 博客转移到 海胖网 http://haipz.com/ 希望你能支持我们! 博客转移到 海胖网 http://haipz.c ...

  8. 全排列算法(字典序法、SJT Algorithm 、Heap's Algorithm)

    一.字典序法 1) 从序列P的右端开始向左扫描,直至找到第一个比其右边数字小的数字,即. 2) 从右边找出所有比大的数中最小的数字,即. 3) 交换与. 4) 将右边的序列翻转,即可得到字典序的下一个 ...

  9. 自定义可判断选项是否正确listbox

    截图如下:        1.实现Converter  获取到listbox,并得到listitem在listbox中的index public class ItemContainerToZIndex ...

  10. 13个SQL优化技巧

    避免无计划的全表扫描<!--?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" ...