- 浏览: 173213 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 ≤ 10^6, 0 < primes[i] < 1000.
类似Ugly number二,创建长度为n的数组,依次生成n个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 ≤ 10^6, 0 < primes[i] < 1000.
类似Ugly number二,创建长度为n的数组,依次生成n个ugly number。代码如下:
public class Solution { public int nthSuperUglyNumber(int n, int[] primes) { int[] count = new int[primes.length]; int[] result = new int[n]; result[0] = 1; int index = 1; while(index < n) { int min = Integer.MAX_VALUE; for(int i = 0; i < count.length; i++) { if(min > result[count[i]] * primes[i]) min = result[count[i]] * primes[i]; } result[index ++] = min; for(int i = 0; i < count.length; i++) { if(min == result[count[i]] * primes[i]) count[i] ++; } } return result[n - 1]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 224Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 223You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 339Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 330Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 448Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 427Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 426The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 384Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 514Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 528Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 366All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 858Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 879Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 552Design and implement a data str ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 761Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 732You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 626For a undirected graph with tre ... -
Bulb Switcher
2016-02-28 12:12 347There are n bulbs that are init ...
相关推荐
各种数据结构、算法及实用的C#源代码 C#,超级阿格里数字(超级丑数,Super Ugly Number)的算法与源...超级阿格里数字(超级丑数,Super Ugly Number)由丑数(Ugly Number)拓展而来,不过其因子质数,是事先给定的。
各种数据结构、算法及实用的C#源代码.C#,阿格里数(Ugly Number)的多种算法与源代码 阿格里数,即丑数(Ugly Number)、逊数(Humble Number)。 一般而言:把只包含质因子2,3和5的数称作丑数(Ugly Number)。...
Super Ugly Number Fraction to Recurring Decimal Factorial Trailing Zeroes Nim Game 模拟 Reverse Integer Palindrome Number Insert Interval Merge Intervals Minimum Window Substring Multiply Strings ...
The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...
算法与数据结构它们分别涵盖了以下主要内容: 数据结构(Data Structures): 逻辑结构:描述数据元素之间的逻辑关系,如线性结构(如数组、链表)、树形结构(如二叉树、堆、B树)、图结构(有向图、无向图等)...
电子邮件模板Gradle SUPER HACKY UGLY LEAKY JavaFX 修补
pku acm1338 Ugly Numbers 代码 动态规划思想,采用链表实现,解题报告请访问:http://blog.csdn.net/china8848
AutoJs源码-wzry.ugly.min.ugly.min.ugly.min.ugly.min.ugly.min.ugly.min。本资源购买前提醒:本源码都是实际autojs项目模板,安装好autojs直接运行即可打开。1、支持低版本autojs。2、资源仅供学习与参考,请勿...
pku acm 1338 Ugly Numbers代码 动态规划思想,数组实现 解题报告请访问:http://blog.csdn.net/china8848
scala-cli-maven-ugly-number 描述 将数字除以2、3和5的最大可除幂,如果数字变为1,则它是一个丑陋的数字,否则不是。 科技栈 OpenJDK的8 Scala 专家 Docker堆栈 docker-cli openjdk:8 要求 必须安装Docker桌面...
新小学英语四年级下册Unit The ugly ducklingPPT课件.pptx
The Good, The Bad, and the ugly, The UNIX Legacy.pdf 专家观点 UNIX的美与丑!
资源来自pypi官网。 资源全名:augly-0.1.9-py3-none-any.whl
scala-cli-sbt-ugly-number-pattern-match描述将数字除以2、3和5的最大可除幂,如果数字变为1,则它是一个丑陋的数字,否则不是。 遵循函数式编程实践。 模式匹配的示例。科技栈OpenJDK的8 ScalasbtDocker堆栈docker...
1201.Ugly-Number-III(待定) (H) (男) (H) Binary Search by Value (男) (H-) (H-) (H) (H-) (H-) (H) (H-) (H-) (H) (H-) (H-) (男) (男) (H-) (M+) (男) (M+) (男) (M+) (H-) (M+
Ugly Face
ugly_num.exe
解决ubuntu系统自带的Rthymbox音乐播放器默认不支持mp3和wma格式音乐的播放
这两天在弄linux下的东西,用的是RedHat EnterPrise 6,但由于其yum源是收费的,就使用的是对应的CentOS6,但更新后发觉安装音乐解码器插件时还是差了这几个包,纠结了好久,终于找齐了,一并传上来,希望能帮到有...
rehel6下安装mp3插件所需要的rpm包,tar.gz解压出来就是rpm包, tar -zxvf gstrem-ugly.tar.gz cd gstream-ugly yum install *.rpm