当前位置:网站首页>LeetCode_ Dynamic programming_ Medium_ 264. Ugly number II
LeetCode_ Dynamic programming_ Medium_ 264. Ugly number II
2022-07-26 02:16:00 【Old street of small town】
1. subject
Give you an integer n , Please find out and go back to n Ugly number .
Ugly number That is, only the prime factor is included 2、3 and / or 5 The positive integer .
Example 1:
Input :n = 10
Output :12
explain :[1, 2, 3, 4, 5, 6, 8, 9, 10, 12] It's from the front 10 A sequence of ugly numbers .
Example 2:
Input :n = 1
Output :1
explain :1 It's usually considered ugly .
Tips :
1 <= n <= 1690
source : Power button (LeetCode)
link :https://leetcode.cn/problems/ugly-number-ii
2. Ideas
(1) The law of violent exhaustion
Violent exhaustion is easier to think of :
① If n == 1, Then return directly 1, because 1 It's usually considered ugly ;
② Otherwise, from a positive integer x = 2 Start judging , For specific judgment methods, please refer to LeetCode_263. Ugly number This article , If the current number is ugly , be n–, When n == 1 Stop judging when , At this time x - 1 That is the first. n Ugly number . However, the time complexity of this method is high , stay LeetCode When submitting on “ Time limit exceeded ” A hint of !
(2) The smallest pile
Train of thought reference Official solution to this problem .
(3) Dynamic programming
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— The law of violent exhaustion
class Solution {
public int nthUglyNumber(int n) {
int[] factors = {
2, 3, 5};
if (n == 1) {
// 1 It's usually considered ugly
return 1;
}
// From positive integers 2 Start judging
int x = 2;
int i = 1;
while (n > 1) {
int tmp = x;
for (int j = 0; j < factors.length; j++) {
while (tmp % factors[j] == 0) {
tmp /= factors[j];
}
}
if (tmp == 1) {
n--;
}
// Determine the next positive integer
x++;
}
return x - 1;
}
}
// Ideas 2———— The smallest pile
class Solution {
public int nthUglyNumber(int n) {
int[] factors = {
2, 3, 5};
if (n == 1) {
// 1 It's usually considered ugly
return 1;
}
Set<Long> hashSet = new HashSet<>();
PriorityQueue<Long> heap = new PriorityQueue<>();
hashSet.add(1L);
// Initially, the heap is empty , First, the smallest ugly number 1 Add heap
heap.offer(1L);
int ugly = 0;
for (int i = 0; i < n; i++) {
// Every time you take out the top element x, be x Is the smallest ugly number in the heap
long x = heap.poll();
ugly = (int) x;
for (int factor : factors) {
/* because 2x, 3x, 5x It's also an ugly number , So it will 2x, 3x, 5x Add heap This will result in duplicate elements in the heap , To avoid repeating elements , Here we use hash set to remove duplication In the case of excluding duplicate elements , The first n The element extracted from the smallest heap for the second time is the n Ugly number */
long next = x * factor;
if (hashSet.add(next)) {
heap.offer(next);
}
}
}
return ugly;
}
}
// Ideas 3———— Dynamic programming
class Solution {
public int nthUglyNumber(int n) {
// dp[i] It means the first one i Ugly number
int[] dp = new int[n + 1];
// The smallest ugly number is 1
dp[1] = 1;
// Define three pointers , Indicates that the next ugly number is the ugly number pointed by the current pointer multiplied by the corresponding prime factor
int p2 = 1, p3 = 1, p5 = 1;
for (int i = 2; i <= n; i++) {
int num2 = dp[p2] * 2, num3 = dp[p3] * 3, num5 = dp[p5] * 5;
dp[i] = Math.min(Math.min(num2, num3), num5);
if (dp[i] == num2) {
p2++;
}
if (dp[i] == num3) {
p3++;
}
if (dp[i] == num5) {
p5++;
}
}
return dp[n];
}
}
边栏推荐
- Navica tool imports remote MySQL into local MySQL database
- [2019] [paper notes] tunable THz broadband absorption based on metamaterials——
- Programming basic environment variable setting of in-house SOC
- SQL manual blind injection and error reporting injection
- 微信小程序解密并拆包获取源码教程
- 一种MCU事件型驱动C框架
- (Dynamic Programming Series) sword finger offer 48. the longest substring without repeated characters
- From a data incremental processing problem, we can see the consciousness gap of technicians
- Be careful about bitmap, the "memory Assassin"~
- 18_ Request file
猜你喜欢

Navica tool imports remote MySQL into local MySQL database

Quick start of adding, deleting, modifying and checking business

Prometheus + redis exporter + grafana monitor redis service

项目管理:精益管理法

TI AM335x工控模块网络跟文件系统NFS的实现

商业智能BI全解析,探寻BI本质与发展趋势

Acwing 379. hide and seek problem solution (minimum path non repeating point coverage)

Bitmap这个“内存刺客”你要小心~

ggplot2学习总结

1. Mx6ul core module use serial TF card read / write test (V)
随机推荐
Common shell operations in Phoenix
Dqn pytoch example
[2019] [paper notes] tunable THz broadband absorption based on metamaterials——
2022.7.25-----leetcode.919
[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——
Error reporting caused by local warehouse
Prometheus+blackbox exporter+grafana monitoring server port and URL address
Sqlyog data import and export graphic tutorial
Obsidian mobile PC segment synchronization
National standard gb28181 protocol video platform easygbs message pop-up mode optimization
商业智能BI全解析,探寻BI本质与发展趋势
租户问题。
Ti AM335X工控模块矩阵键盘电路的设计与驱动移植
I.MX6UL核心模块使用连载-RTC测试 (十二)
What does the Red Cross in the SQL editor mean (toad and waterdrop have been encountered...)
微信小程序解密并拆包获取源码教程
BigDecimal use
Be careful about bitmap, the "memory Assassin"~
uni-app跨域配置
18_ Request file