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. 写过的HTML标签(一)

    HTML >   标题显示字体大小为<h1>. HTML 段落是通过标签 <p> 来定义的. HTML 链接是通过标签 <a> 来定义的.  实例: < ...

  2. 第三回 Bootstrap3.x 起步

    基本模版 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="u ...

  3. MVC 页面跳转

    想要学习MVC,那么页面跳转就是必不可少的,在此我总结了常见的页面跳转方式,给自己理一下做个备份,也顺便和大家分享一下. 常见页面跳转的:(下面的视图名和控制器中的方法名一样) 在控制器内部跳转: / ...

  4. php数据过滤函数与方法示例【转载】

    1.php提交数据过滤的基本原则 1)提交变量进数据库时,我们必须使用addslashes()进行过滤,像我们的注入问题,一个addslashes()也就搞定了.其实在涉及到变量取值时,intval( ...

  5. DNS服务器搭建

    注意正式运行的dns服务器主dns设置 中没有启用转发器,所以部分网页如taobao解析时可能很慢.开启转发器即可转发器地址指向电信dns. [root@master ~]# lsb_release ...

  6. Javascript验证用户输入URL地址是否正确

    <script type="text/javascript">function checkUrl() { var url = document.getElementBy ...

  7. 8个强大的基于Bootstrap的CSS框架

    做过前端开发的小伙伴们应该对Bootstrap不会陌生,它是由Twitter推出的开源CSS框架,其中包含了很多Web前端开发的工具包和应用组件.当然,和jQuery一样,Bootstrap同时也是一 ...

  8. 6款基于SVG的HTML5CSS3应用和动画

    1.CSS3/SVG质感背景小图标 镂空效果图标按钮 今天我们来分享一款用CSS3和SVG实现的质感背景小图标,鼠标滑过图标时出现镂空的效果,并且有质感背景的描边,效果非常不错. 在线演示 源码下载 ...

  9. SQL server基本操作(一)

      --1.create database CREATE DATABASE MyDB GO --2.use database USE MyDB GO --3.create table CREATE T ...

  10. 郑州轻工业OJ1400--这不可能是情书吧

    地址:http://acm.zzuli.edu.cn/problem.php?id=1400 #include<stdio.h> #include<string.h> #inc ...