当前位置:网站首页>LeetCode_动态规划_中等_313.超级丑数
LeetCode_动态规划_中等_313.超级丑数
2022-08-01 12:21:00 【小城老街】
1.题目
超级丑数 是一个正整数,并满足其所有质因数都出现在质数数组 primes 中。
给你一个整数 n 和一个整数数组 primes ,返回第 n 个超级丑数 。
题目数据保证第 n 个超级丑数在 32-bit 带符号整数范围内。
示例 1:
输入:n = 12, primes = [2,7,13,19]
输出:32
解释:给定长度为 4 的质数数组 primes = [2,7,13,19],前 12 个超级丑数序列为:[1,2,4,7,8,13,14,16,19,26,28,32] 。
示例 2:
输入:n = 1, primes = [2,3,5]
输出:1
解释:1 不含质因数,因此它的所有质因数都在质数数组 primes = [2,3,5] 中。
提示:
1 <= n <= 105
1 <= primes.length <= 100
2 <= primes[i] <= 1000
题目数据 保证 primes[i] 是一个质数
primes 中的所有值都互不相同 ,且按递增顺序排列
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/super-ugly-number
2.思路
本题与LeetCode_动态规划_中等_264.丑数 II这题十分相似,只不过本题的质因数是由题目中的数组 primes 给出。
(1)暴力穷举法
暴力穷举法比较容易想到:
① 如果 n == 1,则直接返回 1,因为 1 通常被视为丑数;
② 否则从正整数 x = 2 开始判断,具体的判断方式可以参考263.丑数这篇文章,如果当前的数是丑数,则 n- -,当 n == 1 时停止判断,此时的 x - 1 就是第 n 个丑数。不过该方法的时间复杂度较高,在 LeetCode 上提交时会出现“超出时间限制”的提示!
(2)最小堆
思路参考本题官方题解。
(3)动态规划
思路参考本题官方题解。
相似题目
LeetCode_因式分解_简单_263.丑数
LeetCode_动态规划_中等_264.丑数 II
3.代码实现(Java)
//思路1————暴力穷举法
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
if (n == 1) {
return 1;
}
//从正整数 2 开始判断
int x = 2;
int i = 1;
while (n > 1) {
int tmp = x;
for (int j = 0; j < primes.length; j++) {
while (tmp % primes[j] == 0) {
tmp /= primes[j];
}
}
if (tmp == 1) {
n--;
}
x++;
}
return x - 1;
}
}
//思路2————最小堆
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
if (n == 1) {
return 1;
}
Set<Long> hashSet = new HashSet<>();
// 优先级队列的底层实现原理就是最小堆
PriorityQueue<Long> heap = new PriorityQueue<>();
hashSet.add(1L);
heap.offer(1L);
int res = 0;
for (int i = 0; i < n; i++) {
long x = heap.poll();
res = (int) x;
for (int prime : primes) {
long nextUgly = x * prime;
if (hashSet.add(nextUgly)) {
heap.offer(nextUgly);
}
}
}
return res;
}
}
//思路3————动态规划
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
// dp[i] 表示第 i 个丑数
int[] dp = new int[n + 1];
int m = primes.length;
int[] pointers = new int[m];
int[] nums = new int[m];
// 最小的丑数是 1
Arrays.fill(nums, 1);
for (int i = 1; i <= n; i++) {
int minNum = Arrays.stream(nums).min().getAsInt();
dp[i] = minNum;
for (int j = 0; j < m; j++) {
if (nums[j] == minNum) {
pointers[j]++;
nums[j] = dp[pointers[j]] * primes[j];
}
}
}
return dp[n];
}
}
边栏推荐
- 如何利用DevExpress控件绘制流程图?看完这篇文章就懂了!
- Qt获取文件夹下所有文件
- 测试发文
- pandas connects to the oracle database and pulls the data in the table into the dataframe, filters all the data from the current time (sysdate) to one hour ago (filters the range data of one hour)
- How to use DevExpress controls to draw flowcharts?After reading this article, you will understand!
- sql中ddl和dml(数据库表与视图的区别)
- 通讯录(静态版)(C语言)(VS)
- Grafana 9.0 released, Prometheus and Loki query builders, new navigation, heatmap panels and more!
- 实现集中式身份认证管理的案例
- js中常用追加元素的几种方法:append,appendTo,after,before,insertAfter,insertBefore,appendChild
猜你喜欢
随机推荐
Aeraki Mesh 加入 CNCF 云原生全景图
Programmer's self-cultivation
.NET性能优化-使用SourceGenerator-Logger记录日志
Fault 007: The dexp derivative is inexplicably interrupted
Aeraki Mesh became CNCF sandbox project
大中型网站列表页翻页过多怎么优化?
[5 days countdown] to explore the secret behind the great quality promotion, gift waiting for you to take of $one thousand
如何将第三方服务中心注册集成到 Istio ?
Pytest电商项目实战(下)
通讯录(静态版)(C语言)(VS)
Process sibling data into tree data
Apex installation error
STM32 CAN过滤器配置详解
SCHEMA解惑
【社区明星评选】第24期 8月更文计划 | 笔耕不辍,拒绝躺平!更多原创激励大礼包,还有华为WATCH FIT手表!
pandas连接oracle数据库并拉取表中数据到dataframe中、筛选当前时间(sysdate)到一个小时之前的所有数据(筛选一个小时的范围数据)
博弈论(Depu)与孙子兵法(42/100)
Several methods of appending elements are commonly used in js: append, appendTo, after, before, insertAfter, insertBefore, appendChild
Meshlab&Open3D SOR滤波
STM32 CAN filter configuration details









