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. Student

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace PersonD ...

  2. django 学习-17 Django会话Session

    session   类似于字典的一个对象,是可读可写的 跟cookie的变化不大而且还显得简单 在这里我只说一下改变的地方 1.vim blog/views.py if uf.is_valid():  ...

  3. c#获取网页内容的三种方法

    1.webclient WebClient wc = new WebClient(); Stream stm = wc.OpenRead(str_url); StreamReader sr = new ...

  4. ubuntu 删除除了某个文件或文件夹之外的所有文件或者目录

    今天需要将网站根目录下的所有文件全部删除但是还需要保留phpmyadmin这个文件夹,本来是可以一个一个删除的,后来想应该会有 一个命令是可以用一个命令删除除了phpmyadmin之外的所有文件和文件 ...

  5. XenApp简单部署

    作者:MR.Yangwj 目录 XenApp简单部署... 1 一.         XenApp安装... 1 (一)      服务器配置任务... 9 1)     许可证服务器配置... 9 ...

  6. Webservice发布出现 测试窗体只能用于来自本地计算机的请求

    今天发布了一个接口,一开始以为是.netframework版本的问题,从3.5降到2.0到服务器发布,发布后还是会出现 测试窗体只能用于来自本地计算机的请求 上网查找资料发现原来是 webconfig ...

  7. c 语言时间的输出和比较

    time_t The most basic representation of a date and time is the type time_t. The value of a time_t va ...

  8. Stack Overflow 2016最新架构探秘

    这篇文章主要揭秘 Stack Overflow 截止到 2016 年的技术架构. 首先给出一个直观的数据,让大家有个初步的印象. 相比于 2013 年 11 月,Stack Overflow 在 20 ...

  9. (转).NET Memory Profiler 使用简介

    1         简介 .Net Memory Profiler(以下简称Profiler):专门针对于.NET程序,功能最全的内存分析工具,最大的特点是具有内存动态分析(Automatic Mem ...

  10. 一些常用css技巧的为什么(一)我所理解的margin

    要用到的基本术语和概念: 正常流:HTML文档的文本布局,在非西方语言中流的方向可能不同.大多数元素都在正常流中,浮动或定位可以让元素脱离正常流. 块级元素:像p,div之类的元素在正常流中会在其框之 ...