当前位置:网站首页>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];
}
}
边栏推荐
- Advantages of composition API
- 3. Upload the avatar to qiniu cloud and display it
- Detailed explanation of redis6.x configuration parameters
- SQL how to return all data when the input query condition is empty
- 2022.7.25-----leetcode.919
- Implementation of C iterator
- Ti AM335X工控模块矩阵键盘电路的设计与驱动移植
- A MCU event driven C framework
- (CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
- Implementation of Ti am335x industrial control module network and file system nfs
猜你喜欢

(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud

I.MX6UL核心模块使用连载-USB接口测试 (六)
![[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——](/img/5d/7d26e2d0d832c95e1cc011995ce774.png)
[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——

我来图书馆小程序签到流程分析

【2021】【论文笔记】6G技术愿景——OTFS调制技术

i. Mx6ull snvs power domain GPIO status hold verification

Sqlyog data import and export graphic tutorial

Characteristics and determination of neuraminidase from Clostridium perfringens in Worthington

Ti AM335X工控模块使用beaglebone(bbb)的Debian系统

1. Mx6ul core module uses serial EMMC read / write test (IV)
随机推荐
【2021】【论文笔记】红外及THz下的细胞膜生物效应——效应是现象,作用是机理——THz对医学的好处
The slow loading of the first entry page of vite local operation
Ggplot2 learning summary
mysql 事务隔离级别
I.MX6UL核心模块使用连载-以太网测试 (七)
19_ Request forms and documents
[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——
TCP three handshakes and four waves
Composition API的优势
c# 单元测试
BigDecimal use
Tenant issues.
17.反转链表
[2019] [paper notes] tunable THz broadband absorption based on metamaterials——
prometheus+redis-exporter+grafana 监控redis服务
【红队】ATT&CK - 利用BITS服务实现持久化
Be careful about bitmap, the "memory Assassin"~
I.MX6UL核心模块使用连载-触摸屏校准 (九)
[xxl-job] xxl-job learning
Build embedded development environment and FRP penetration under win