力不从心 Leetcode(ugly number heap) 263, 264,313
Leetcode ugly number set (3 now)
new ugly number is generated by multiplying a prime with previous generated ugly numbe
Is ugly number or not
class Solution {
public boolean isUgly(int num) {
if(num==0) return false;
//how to generate the number
while(num%2==0) num/=2;
while(num%3==0) num/=3;
while(num%5==0) num/=5;
return num==1;
}
}
//simulate the process to generate the ugly number
--------------------------------------------------------
next level
class Solution {
public int nthUglyNumber(int n) {
// can store the samle element
//use long to safe
PriorityQueue<Long> pq = new PriorityQueue<>();
pq.offer(1L);
int i = 2;
while(i<=n){
long smallest = pq.poll();
while(!pq.isEmpty() && smallest == pq.peek()) pq.poll();
pq.offer(smallest*2);
pq.offer(smallest*3);
pq.offer(smallest*5);
i++;
}
return pq.poll().intValue();
}
}
Key point: use the feature of PriorityQueue: take the min heap.
- PQ could have duplictae number
- Integer is not enough for this problem (negative will affect the PQ)
--------------------------------
next level there are some methods
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
// can store the samle element
//use long to safe
PriorityQueue<Long> pq = new PriorityQueue<>();
pq.offer(1L);
int i = 2;
while(i<=n){
long smallest = pq.poll();
while(!pq.isEmpty() && smallest == pq.peek()) pq.poll();
for(int j = 0; j<primes.length; j++){
pq.offer(smallest * primes[j]);
}
i++;
}
return pq.poll().intValue();
}
}
https://leetcode.com/problems/super-ugly-number/discuss/76291/Java-three-methods-23ms-36-ms-58ms(with-heap)-performance-explained -- mark to learn
力不从心 Leetcode(ugly number heap) 263, 264,313的更多相关文章
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- [LeetCode] Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number
Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...
- [LeetCode] Ugly Number II (A New Question Added Today)
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number (A New Question Added Today)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- LeetCode——Ugly Number
Description: Write a program to check whether a given number is an ugly number. Ugly numbers are pos ...
- LeetCode() Ugly Number II 背下来!
一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.s ...
- LeetCode Ugly Number (简单题)
题意: 判断是一个数的质因子仅含有2,3,5这3个. 思路: 因子2比较容易解决,num/=num-(num&num-1)就可以了.3和5的需要通过循环来另判. C++ class Solut ...
随机推荐
- Python中的数据类型和数据结构
一.数据类型 Python中有六个标准数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) 其中,除列表Lis ...
- mapreduce去重
现有一个某电商网站的数据文件,名为buyer_favorite1,记录了用户收藏的商品以及收藏的日期,文件buyer_favorite1中包含(用户id,商品id,收藏日期)三个字段,数据内容以“\t ...
- my.变身卡
中级 1620 (4--6级) 高级 8323 (7--9级) 赤炎: 1.老鼠精 135 / 150 2.白骨精 750 3.情丝娘子 264 / 294 4.沙和尚 500 5.九头虫 1835 ...
- python函数基础学习
函数的定义与调用: def 函数名(参数1,参数2): ‘’’函数注释’’’ print(‘函数体’) return 返回值 定 义:def关键字开关,空格之后接函数名和圆括号,最后冒号结尾 def ...
- 3d Max 2018安装失败怎样卸载3dsmax?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- (转)vim(vi)常用操作及记忆方法
vim(vi)常用操作及记忆方法 原文:https://www.cnblogs.com/doseoer/p/6241443.html vi(vim)可以说是linux中用得最多的工具了,不管你配置服务 ...
- MATLAB顺序结构程序和switch实现选择结构
数据操作 (1)数据输入: A=input(提示信息,选项) (2)数据输出: disp(输出项) (3)程序暂停 pause(延迟秒数)若无内容,则需用户按任意键继续 3.2if语句 整非零为真 矩 ...
- Git常用配置
Git设置默认用户名和密码 1.进入C:\users\Administrator目录下,通过git bash终端输入touch .git-credentials后回车2.打开生成的.git-crede ...
- linux_api之文件属性
本篇索引:1.引言2.文件类型3.获取文件属性的函数,stat.fstat.lstat4.超级用户(root用户)和普通用户5.进程与用户ID6.文件权限的检查7.新创建的的文件和目录的所有权8.ac ...
- 【LDAP】LDAP介绍
原文:http://ldapman.org/articles/intro_to_ldap.html原文作者:Michael Donnelly 什么是LDAP? LDAP的英文全称是Lightweigh ...