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的更多相关文章

  1. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  2. Leetcode 313. super ugly number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  3. [LintCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  4. 313. Super Ugly Number

    题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...

  5. [LeetCode] Super Ugly Number (Medium)

    Super Ugly Number 最后WA没做出来. typedef long long int64; #define MAXBOUND 10000000 class Solution { publ ...

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

  7. [Swift]LeetCode313. 超级丑数 | Super Ugly Number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

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

  9. Super Ugly Number -- LeetCode

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  10. [LeetCode] 313. Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

随机推荐

  1. android中versionCode&versionName

    原文来自:http://blog.csdn.net/wh_19910525/article/details/8660416 ,略有修改 一.概述 Android的版本可以在androidmainfes ...

  2. CodeForces 219D 树形DP

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  3. svn版本升级遇到的bug

    从svn1.0升级到svn10.x时发生的bug 1.可能是没有javahl这个架包或是与你升级后的svn版本不匹配,eclipse在线下载就好 2.项目因为之前连接是较低版本的svn,当svn升级后 ...

  4. 4_STL设计理念_算法

    STL算法,容器,迭代器的设计理念1.STL容器通过 类模板 技术,实现 数据类型 和 容器模型的分离:2.迭代器技术 实现了 遍历和操作容器的统一方法3.STL算法设计理念:通过预定义的函数对象和函 ...

  5. mysql批量替换指定字符串

    将字段title中的abc替换为123 UPDATE `table1` SET `title` = replace(title, 'abc', '123') WHERE `typeid` =18;

  6. 设置mariadb字符集为utf8

    我用的是10.0.25 mariadb,     在centos7系统上,用以下指令设置数据库字符集. [client] default-character-set=utf8 [mysql] defa ...

  7. html代码中的form参数是基本一致的

    由于pear的大多数模块仍处于开发当中,因此,这里列举的是随着php4.05一起发布的pear中的模块,需要注意的是,一些抽象类或者是基类(如mail.php,log.php,cache.php)没有 ...

  8. 【iCore3 双核心板_FPGA】例程十二:Modelsim仿真实验

    实验指导书及代码包下载: http://pan.baidu.com/s/1bnQEldl iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  9. Windows7如何安装Sqlite3

    Sqlite官网地址:http://www.sqlite.org/ Sqlite3文件下载: 1.下载 sqlite-dll-win32-x86-3140100 2.下载 sqlite-tools-w ...

  10. .htaccess 重写去index.php

    .htaccess 重写去index.php <IfModule mod_rewrite.c>RewriteEngine onRewriteCond %{REQUEST_FILENAME} ...