Super Ugly Number
eg 2,3,5
把第一个元素(2,1)放到最小堆,2表示乘积,1表示乘数
乘数 队列 最小堆 即将进行的操作
第一阶段 【1】 【2,3,5】 2 (2,1)出堆,(3,1)入堆,生成新的乘数2,并且(2*2,2)入堆;
第二阶段 【1】 【3,5】 3,4 (3,1)出堆,(5,1)入堆,生成新的乘数3,并且(3*2,2)入堆 ;
【2】 【2,3,5】
第三阶段 【1】 【5】 5,4,6 (4,2)出堆,(5,1)入堆,生成新的乘数3,并且(3*2,2)入堆 ;
【2】 【2,3,5】
【3】 【2,3,5】
注意(1) 重复数据!(2)上面的队列固定,即将访问的位置是通过下标来记录的,即代码中的start_idx,记录乘数-位置之间的map关系
#include<vector>
#include<queue>
#include<map>
#include<limits>
#include<iostream>
using namespace std;
struct Node{ //记录这个乘积,是由哪个乘数得到的
long idx; //乘数
long val; //乘积
};
struct cmp
{ bool operator()(const Node &a,const Node &b)
{
return a.val>b.val;
}
};
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
priority_queue<Node, vector<Node>, cmp> min_heap; if(primes.size() == ) return ;
map<long , unsigned int> start_idx;//记录该乘数对应的队列访问到primes第几个元素了
start_idx[] = ;
int res = ; Node temp_node;
temp_node.idx = ; //idx is the first val of start_idx
temp_node.val = primes[];
min_heap.push(temp_node); int cnt = ;
if (n == ) return ;
long min_val;
long min_idx = ;
while (cnt < n)
{
min_val = min_heap.top().val;
min_idx = min_heap.top().idx;
min_heap.pop();
if ((res != (min_val))) //去重
{
res = min_val;
cnt++;
start_idx[min_val] = ;
temp_node.idx = min_val; //idx is the first val of start_idx
temp_node.val = min_val * primes[];
min_heap.push(temp_node);
} if ((start_idx[min_idx] < primes.size() - ) )
{
start_idx[min_idx] ++;
temp_node.idx = min_idx; //idx is the first val of start_idx
temp_node.val = min_idx * primes[start_idx[min_idx]];
min_heap.push(temp_node);
}
}
return res; }
};
Super Ugly Number的更多相关文章
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- Leetcode 313. super ugly number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LintCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 313. Super Ugly Number
题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...
- [LeetCode] Super Ugly Number (Medium)
Super Ugly Number 最后WA没做出来. typedef long long int64; #define MAXBOUND 10000000 class Solution { publ ...
- Ugly Number,Ugly Number II,Super Ugly Number
一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...
- [Swift]LeetCode313. 超级丑数 | Super Ugly Number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- Super Ugly Number -- LeetCode
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] 313. Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
随机推荐
- Hibernate中易错地方的总结
1.Hibernate中的配置文件要放在src下,注意不能放在包目录下 2.Hibernate中@Before @After方法不能再普通的类里用,只有在专门的JUnit测试用例里面用. 3.使用 ...
- 解决mysql5.6+在zabbix监控中执行脚本出现密码的错误问题
1.mysql命令行中授权mysql监控所需的账号和密码(权限select权限即可) 2.通过mysql_config_editor 配置登录问题: [root@back_zabbix_100 scr ...
- c# 复习
一.输入输出语句 二.异常语句 try catch fina 保护程序,在出错的情况下也不会终止. 三.输入一个日期,判断是否正确. 每日一语:觉得自己做得到和做不到,其实只在一念之间.
- spring security 控制用户信息用户加密 缓存用户信息
1. MD5加密 任何一个正式的企业应用中,都不会在数据库中使用明文来保存密码的,我们在之前的章节中都是为了方便起见没有对数据库中的用户密码进行加密,这在实际应用中是极为幼稚的做法.可以想象一下,只要 ...
- bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得
这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...
- FTP目录或文件名有中文时导致,下载失败的问题
在FTPClient ftpClient = new FTPClient()代码后, ftpClient.connect(url,port)代码前, 调用ftpClient.setAutodetect ...
- MyEclipse tomcat7.x 自定义项目部署路径
- HTTP Error 500 - Server Error.
Error Details: The FastCGI process exited unexpectedly Error Number: -1073741819 (0xc0000005). Error ...
- mysql替换制定的内容的 类似正则表达式的功能
content= 'asnfojassozxpdsgdspdps神龙架谁骄傲的骄傲搜ID飞机扫' SELECT content FROM test WHERE id =1 吧zx替换成ZZZZ UPD ...
- LNMP环境简易安装流程
1.关闭防火墙 [root@CentOS ~]# chkconfig iptables off 2.关闭selinux vi /etc/sysconfig/selinux //将SELINUX=enf ...