当前位置:网站首页>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];
}
}
边栏推荐
- Towhee 每周模型
- sql中ddl和dml(数据库表与视图的区别)
- Several methods of appending elements are commonly used in js: append, appendTo, after, before, insertAfter, insertBefore, appendChild
- CloudCompare & PCL ICP registration (point to face)
- 2022 Go生态圈 rpc 框架 Benchmark
- Visualization of lag correlation of two time series data in R language: use the ccf function of the forecast package to draw the cross-correlation function, and analyze the lag correlation according t
- MVVM响应式
- Js手写函数之new的模拟实现
- 【云享新鲜】社区周刊·Vol.73- DTSE Tech Talk:1小时深度解读SaaS应用系统设计
- bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)
猜你喜欢

通讯录(静态版)(C语言)(VS)

初级必备:单例模式的7个问题

Pytest电商项目实战(下)

并发编程10大坑,你踩过几个?

一文带你读懂云原生、微服务与高可用

判断JS数据类型的四种方法

Programmer's self-cultivation

Fault 007: The dexp derivative is inexplicably interrupted
![[5 days countdown] to explore the secret behind the great quality promotion, gift waiting for you to take of $one thousand](/img/de/1e6069e84183d1400c90a6ec574f72.png)
[5 days countdown] to explore the secret behind the great quality promotion, gift waiting for you to take of $one thousand

阿里云官方 Redis 开发规范
随机推荐
SQL函数 %SQLUPPER
Complete Raiders of JS Data Type Conversion
腾讯云原生:Areaki Mesh 在 2022 冬奥会视频直播应用中的服务网格实践
Several methods of appending elements are commonly used in js: append, appendTo, after, before, insertAfter, insertBefore, appendChild
英特尔全方位打造算力基础,助推“算”赋百业
R语言诊断ARIMA模型:forecast包构建了一个ARIMA模型、使用checkresiduals函数诊断ARIMA模型、并进行结果解读(拟合较差的ARIMA模型具有的特点)
R语言拟合ARIMA模型:使用forecast包中的auto.arima函数自动搜索最佳参数组合、模型阶数(p,d,q)、设置seasonal参数指定在模型中是否包含季节信息
R语言检验时间序列的平稳性:使用tseries包的adf.test函数实现增强的Dickey-Fuller(ADF)检验、检验时序数据是否具有均值回归特性(平稳性)、具有均值回归特性的案例
The CAN communication standard frame and extended frame is introduced
SQL函数 STR
数字证书原理
[CLion] CLion always prompts "This file does not belong to any project target xxx" solution
并发编程10大坑,你踩过几个?
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化密度图、使用stat_central_tendency函数在密度中添加均值竖线并自定义线条类型
Detailed explanation of table join
故障007:dexp导数莫名中断
Apex installation error
[Community Star Selection] Issue 24 August Update Plan | Keep writing, refuse to lie down!More original incentive packages, as well as Huawei WATCH FIT watches!
Visualization of lag correlation of two time series data in R language: use the ccf function of the forecast package to draw the cross-correlation function, and analyze the lag correlation according t
【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用