1. Ugly Number II

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

Example:

Input: n = 10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note:

  1. 1 is typically treated as an ugly number.
  2. n does not exceed 1690.

解法1

暴力搜索。假设已经知道了前n个丑数\(a_1, a_2, ..., a_n\),求第n+1个丑数,则有:

\[a_{n+1} = \min\{2*a_i, 3*a_j, 5*a_k\}, \quad \forall i, j, k\in[1, n]\\
s.t\quad a_{n+1} > a_n
\]
class Solution {
public:
int nthUglyNumber(int n) {
vector<int>u_n{1};
int prime[3] = {2, 3, 5};
while(u_n.size() < n){
int flag = 0;
int cur_n = INT_MAX;
for(int i = u_n.size() - 1; i >= 0; --i){
for(int j = 0; j < 3; ++j){
if(u_n[i]*prime[j] > u_n.back()){
cur_n = min(cur_n, u_n[i]*prime[j]);
}else{
flag++;
}
}
if(flag == 3)break;
}
u_n.push_back(cur_n);
}
return u_n.back();
}
};

但是提交会超时。。。

解法2 对解法1进行改进。显然丑数数组是有序的,可以用二分查找完成,查找的问题描述为:

寻找第一次出现的满足 \(k\times a_i > a_n\)的\(a_i\)

搜索过程为:对于区间\([l, r]\)

  • \(k\times a_{mid} > a_n\),则满足条件的肯定在\([l, mid]\)中
  • \(k\times a_{mid} \leq a_n\),则满足条件的肯定在\([mid+1, r]\)中
typedef long long int LL;
class Solution {
public:
int nthUglyNumber(int n) {
vector<LL>u_n{1};
int prime[3] = {2, 3, 5};
while(u_n.size() < n){
LL cur_n = LLONG_MAX;
for(int j = 0; j < 3; ++j){
int idx = bin_search(u_n, prime[j]);
cur_n = min(cur_n, prime[j]*u_n[idx]);
}
u_n.push_back(cur_n);
}
return u_n.back();
}
int bin_search(vector<LL>&nums, int k){
int last_num = nums.back();
int l = 0, r = nums.size()-1;
while(l < r){
int mid = (l + r) / 2;
if(nums[mid]*k > last_num)r=mid;
else l = mid + 1;
}
return l;
}
};

解法3 注意到事实:如果\(a_n\)是丑数,则\(2a_n, 3a_n, 5a_n\)也是丑数

typedef long long int LL;
class Solution {
public:
int nthUglyNumber(int n) {
priority_queue<LL, vector<LL>, greater<LL>>q;
set<LL>s;
int prime[3] = {2, 3, 5}; q.push(1);
s.insert(1);
LL ans = q.top();
for(int i = 0; i < n; ++i){
ans = q.top();
q.pop();
s.erase(ans);
for(int j = 0; j < 3; ++j){
if(s.find(ans*prime[j]) == s.end()){
q.push(ans*prime[j]);
s.insert(ans*prime[j]);
}
}
}
return ans;
}
};

解法4 根据解法三种事实,利用动态规划

class Solution {
public:
int nthUglyNumber(int n) {
int pre2 = 0, pre3 = 0, pre5 = 0;
int nums[1690];
nums[0] = 1;
for(int i = 1; i < n; ++i){
int ugly = min(nums[pre2]*2, min(nums[pre3]*3, nums[pre5]*5));
nums[i] = ugly;
if(ugly % 2 == 0)pre2++;
if(ugly % 3 == 0)pre3++;
if(ugly % 5 == 0)pre5++;
}
return nums[n-1];
}
};

【刷题-LeetCode】264. Ugly Number II的更多相关文章

  1. [leetcode] 264. Ugly Number II (medium)

    263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...

  2. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  3. [LeetCode] 264. Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  4. Leetcode 264. Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  5. (medium)LeetCode 264.Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  6. LeetCode——264. Ugly Number II

    题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...

  7. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  8. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  9. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

随机推荐

  1. CF1106A Lunar New Year and Cross Counting 题解

    Content 试求出在一个 \(n\times n\) 的地图 \(M\) 中,满足 \(1\leqslant i,j\leqslant n\) 且 \(M_{i,j}=M_{i+1,j+1}=M_ ...

  2. word里搜狗输入法出不来,可以按ctrl+空格键

    word里搜狗输入法出不来,可以按ctrl+空格键

  3. 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...

  4. 【LeetCode】 258. Add Digits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...

  5. 【LeetCode】170. Two Sum III - Data structure design 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组+字典 平衡查找树+双指针 日期 题目地址:htt ...

  6. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  7. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  8. RabbitMQ学习笔记二:Java实现RabbitMQ

    本地安装好RabbitMQ Server后,就可以在Java语言中使用RabbitMQ了. RabbitMQ是一个消息代理,从"生产者"接收消息并传递消息至"消费者&qu ...

  9. MongoDB基本介绍与安装(1)

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功 ...

  10. 替代联阳IT6564方案|CS5262替代IT6564|设计DP转HDMI+VGA扩展坞方案

    联阳IT6564:带嵌入式MCU的单芯片4通道DisplayPort1.2到HDMI2.0/VGA转换器 联阳IT6564是一种高性能的单芯片显示端口到HDMI和VGA转换器.IT6564FN结合Di ...