当前位置:网站首页>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];
}
}
边栏推荐
- SQL函数 SQRT
- A new generation of ultra-safe cellular batteries, Sihao Airun goes on sale starting at 139,900 yuan
- 2022 Go生态圈 rpc 框架 Benchmark
- 【CLion】CLion 总是提示 “This file does not belong to any project target xxx” 的解决方法
- The difference between undefined and null in JS
- win10系统重装,无法登录进行同步的情况下chrome数据恢复
- Several methods of appending elements are commonly used in js: append, appendTo, after, before, insertAfter, insertBefore, appendChild
- The use of Ts - Map type
- Transfer learning to freeze the network:
- CAN通信标准帧和扩展帧介绍
猜你喜欢
随机推荐
【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
博弈论(Depu)与孙子兵法(42/100)
一文带你彻底厘清 Kubernetes 中的证书工作机制
How to integrate 3rd party service center registration into Istio?
Envoy 源码流程图
Beyond Compare 4 试用期到期
Js手写函数之new的模拟实现
[Unity3D Plugin] AVPro Video Plugin Share "Video Player Plugin"
The difference between undefined and null in JS
【云享新鲜】社区周刊·Vol.73- DTSE Tech Talk:1小时深度解读SaaS应用系统设计
【CLion】CLion 总是提示 “This file does not belong to any project target xxx” 的解决方法
R language fitting ARIMA model: use the auto.arima function in the forecast package to automatically search for the best parameter combination, model order (p, d, q), set the seasonal parameter to spe
R language ggplot2 visualization: use the ggdensity function of the ggpubr package to visualize density plots, use the stat_central_tendency function to add mean vertical lines to the density and cust
SQL函数 STR
Favorites|Mechanical Engineer Interview Frequently Asked Questions
What is MNIST (what does plist mean)
SQL函数 SQUARE
测试发文
Aeraki Mesh 加入 CNCF 云原生全景图
STM32 CAN filter configuration details