Super Ugly Number -- LeetCode
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 ≤ 106, 0 < primes[i] < 1000.
思路:解法和ugly number II一样。复杂度O(kn)。
这个算法有一个改进的地方,min函数可以用最小堆来实现。这样复杂度是O(log(k)n)。
class Solution {
public:
int min(vector<int>& primes, vector<int>& pointers, vector<int>& superUglyNumber) {
int res = INT_MAX;
for (int i = , n = primes.size(); i < n; i++)
if (res > superUglyNumber[pointers[i]] * primes[i])
res = superUglyNumber[pointers[i]] * primes[i];
for (int i = , n = primes.size(); i < n; i++)
if (res == superUglyNumber[pointers[i]] * primes[i])
pointers[i]++;
return res;
}
int nthSuperUglyNumber(int n, vector<int>& primes) {
vector<int> superUglyNumber;
superUglyNumber.push_back();
vector<int> pointers(primes.size());
for (int i = ; i < n; i++)
superUglyNumber.push_back(min(primes, pointers, superUglyNumber));
return superUglyNumber.back();
}
};
Super Ugly Number -- LeetCode的更多相关文章
- [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 ...
- Leetcode 313. super ugly number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] Super Ugly Number (Medium)
Super Ugly Number 最后WA没做出来. typedef long long int64; #define MAXBOUND 10000000 class Solution { publ ...
- 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 ...
- [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 ...
- [Swift]LeetCode313. 超级丑数 | Super Ugly Number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 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 ...
随机推荐
- typeAliasesPackage 配置
mybatis 的 xml 文件中需要写类的全限定名,较繁琐,可以配置自动扫描包路径给类配置别名,有两种配置方式. 方式一: mybatis-config.xml 中配置 <typeAliase ...
- WIN10把照片查看器设为默认看图软件
WIN10默认是PHOTO,没有以前WIN7的照片查看器好用,要改回来的方法如下: 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Photo ...
- Kotlin中功能操作与集合(KAD 11)
作者:Antonio Leiva 时间:Feb 2, 2017 原文链接:https://antonioleiva.com/functional-operations-collections-kotl ...
- linux path环境变量基础
系统环境变量与个人环境变量的配置文件 系统级别的配置文件: /etc/profile :这个文件预设了几个重要的变量,例如PATH, USER, LOGNAME, MAIL, INPUTRC, HO ...
- 验证表单的js代码段
JS常用功能 转载自:http://blog.csdn.net/kalision/article/details/12516103 引用js文件: <script src="js/d ...
- wireshark简单使用
过滤表达式的规则 表达式规则 1. 协议过滤 比如TCP,只显示TCP协议. ip.src == 219.216.87.200 and ip.dst==219.216.87.254 2 ...
- 再理一下prerouting和postrouting等插入点
这些地方的准确翻译是hook点(hook点是一个土的说法,学名叫rule chain,规则链)这些规则链是内核netfilter架构布置在内核里面的,然后iptables是利用了这套基础架构,想起了内 ...
- 【Luogu】P3288方伯伯运椰子(消圈定理)
题目链接 分数规划题,详见luogu题解 #include<cstdio> #include<cstring> #include<cctype> #include& ...
- linux中sed工具的使用
sed 本身也是一个管线命令,而且 sed 还可以将数据进行取代.删除.新增.撷取特定行等等的功能. $ sed [-nefr] [动作] 选项与参数: -n :使用安静(silent)模式.在一般 ...
- Codeforces Round #359 (Div. 2) B
B. Little Robber Girl's Zoo time limit per test 2 seconds memory limit per test 256 megabytes input ...