当前位置:网站首页>0动态规划 LeetCodde313. 超级丑数
0动态规划 LeetCodde313. 超级丑数
2022-07-23 05:45:00 【18阿鲁】
分析
动态规划 多路归并
丑数」都是基于「已有丑数」而来(使用「已有丑数」乘上「给定质因数」primes[i]primes[i])。
超级丑数的生成可以看作是如下三个序列合并成有序数组的过程。
因此,如果我们所有丑数的有序序列为 a1,a2,a3,…,ana1,a2,a3,…,an 的话,序列中的每一个数都必然能够被以下三个序列(中的至少一个)覆盖(这里假设 primes = [2,3,5]primes=[2,3,5]):
由丑数 * 22 所得的有序序列:1 * 21∗2、2 * 22∗2、3 * 23∗2、4 * 24∗2、5 * 25∗2、6 * 26∗2、8 * 28∗2 …
由丑数 * 33 所得的有序序列:1 * 31∗3、2 * 32∗3、3 * 33∗3、4 * 34∗3、5 * 35∗3、6 * 36∗3、8 * 38∗3 …
由丑数 * 55 所得的有序序列:1 * 51∗5、2 * 52∗5、3 * 53∗5、4 * 54∗5、5 * 55∗5、6 * 56∗5、8 * 58∗5 …
两个相同大小的超级丑数会相遇,一起给结果数组赋值,因此不用担心会重复。
https://leetcode.cn/problems/super-ugly-number/solution/gong-shui-san-xie-yi-ti-shuang-jie-you-x-jyow/
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
int m = primes.length;
int[] ugly = new int[m];
long[] ans = new long[n];
ans[0] = 1;
for (int i = 1; i < n; i++) {
int tmp = Integer.MAX_VALUE;
for (int j = 0; j < m; j++) {
tmp = (int)Math.min(tmp,ans[ugly[j]] * primes[j]);
}
ans[i] = tmp;
for (int j = 0; j < m; j++) {
if (tmp == ans[ugly[j]] * primes[j]) {
ugly[j] += 1;
}
}
}
return (int)ans[n-1];
}
}
边栏推荐
- Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 3
- 配置TX1的系统 + 设为固态盘启动
- Prometheus
- Anonymous upper computer V7 waveform display
- [AUTOSAR candrive 1. learn the function and structure of candrive]
- 《Kubernetes in Action》第二章笔记
- 线程池总结
- 【AUTOSAR CanDrive 2.了解通信Hoh、CanId与PduID的Mapping关系】
- 5.4 Pyinstaller库安装与使用
- Vs attribute configuration related knowledge
猜你喜欢

二叉树的实现-c

Uni native plug-in development -- Youmeng one click login

常见的排序方法—选择排序
![[AUTOSAR cantp 1. learn the network layer protocol of UDS diagnosis]](/img/dc/51a4f091c623dbaaa4c174ebdae92a.png)
[AUTOSAR cantp 1. learn the network layer protocol of UDS diagnosis]

【AUTOSAR DCM 1.模块简介(DSL,DSD,DSP)】

Enter the triangle side length and calculate the area

Unity在URP管线下使用TriLib插件加载模型材质不正确的问题

Navicat for MySQL 安装教程

输入三角形边长,求面积

Analysis of 100 questions and answers in Higher Algebra
随机推荐
[AUTOSAR candrive 1. learn the function and structure of candrive]
使用InfluxDB数据库的疑惑
*offer--2
Uni native plug-in development -- Youmeng one click login
Unity鼠标控制摄像机拖拽、旋转、缩放(模拟编辑器摄像机功能)
linkerd服务网格调研笔记
二叉树基础oj练习-
Unity3D高清渲染管线无法在模型上播放视频
大小写字母转换
TeX or LaTeX or MikTeX or TeX Live or CTeX
5.4 installation and use of pyinstaller Library
Awk programming language
flask项目celery使用redis sentinel中遇到的坑
C语言:基于顺序表的学生管理系统,超级详细,全部都有注释,看完不懂来扇我。
Examen des principes fondamentaux de la structure en acier
表格个人简历
unity3d:向量计算,AOE图形相交
Axure实现增删改查
Basic OJ exercise of binary tree-
嵌入式从入门到精通(入土)——超详细知识点分享1