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. 1166 矩阵取数游戏[区间dp+高精度]

    1166 矩阵取数游戏 2007年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description [ ...

  2. VB.NET 小程序 2

    Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ...

  3. javascript-函数的参数和return语句

    × 目录 [1]参数 [2]Arguments对象 [3]函数重载 [4]return 语句 ------------------------------------- 一.参数(最多25个) 可以动 ...

  4. Sublime Text—自带快捷键介绍

    前言: Sublime Text是个小巧便捷的编辑器,除了众多好用的插件外,还有它自带的快捷键,打代码事半功倍,不会用的赶紧看看吧! 其实菜单上都有,看不懂可以汉化,Key Bindings-Defa ...

  5. Hive通过查询语句向表中插入数据注意事项

    最近在学习使用Hive(版本0.13.1)的过程中,发现了一些坑,它们或许是Hive提倡的比关系数据库更加自由的体现(同时引来一些问题),或许是一些bug.总而言之,这些都需要使用Hive的开发人员额 ...

  6. JQuery 动态添加onclick事件

    $('#div_id').click(function(){ show(1,2,this); });

  7. 动态调用webservice时 ServiceDescriptionImporter类在vs2010无法引用的解决方法

    [导读]ServiceDescriptionImporter是创建Web Service 时使用的类,它是引用继承System.Web.Services 当我将VS2005里写的一段代码放在VS201 ...

  8. iOS - 表格

    一. TableView 1.1 StoryBoard方式 1.2 nib方式 1.2.1 一般 1.2.2 自定义单元格 1.3 纯代码方式 (1) 简单表视图操作 Step1: 实现协议 2个协议 ...

  9. 【iOS基础学习随笔-1】-基于对象的程序设计

    一.对象: 1.在基于对象的程序设计中,一个程序分解成若干个不同的对象,每个对象都有自己独有的能力. 2.一个生产线上的一个工位只负责做好一件事.如果生产出的汽车的车门没有漆好,那问题很可能出在负责上 ...

  10. iOS人机界面指南(翻译)

    本文源自于苹果开发者网站的文章iOS Human Interface Guidelines,内容比较多,此处仅仅是部分笔记.