Leetcode[丑数]-DP&&最小堆&&暴力枚举
题目:
给你一个整数 n ,请你找出并返回第 n 个 丑数 。
丑数 就是只包含质因数 2、3 和/或 5 的正整数。
示例 1:
输入:n = 10
输出:12
解释:[1, 2, 3, 4, 5, 6, 8, 9, 10, 12] 是由前 10 个丑数组成的序列。
示例 2:
输入:n = 1
输出:1
解释:1 通常被视为丑数。
提示:
1 <= n <= 1690
1.暴力枚举:
最容易想到的就是暴力枚举,开辟一个大小为1690的数组ugly[1690],从num=1开始, 判断num是否为丑数,如果是,则存入数组。直到数组中的元素数目为n, 并返回ugly[n-1],但是由于枚举的效率过低,提交时会TLE.
但是这种方案也并非不能用,可以先把1690个丑数打印出来,在定义ugly[1690]时直接给数组赋值,就得了时间复杂度为O(1)的算法(其实就是打表)
int isUgle(long long int n)
{
if(n<=0)return 0;
while(n!=1)
{
if(n%2==0)n=n/2;
else if(n%3==0)n=n/3;
else if(n%5==0)n=n/5;
else return 0;
}
if(n)return 1;
return 0;
}
int nthUglyNumber(int n)
{
int number[1695];
int count=0,num=1;
while(count<n)
{
if(isUgle(num))number[count++]=num;
num++;
}
return number[n-1];
}
2.最小堆
要得到从小到大的第 nn 个丑数,可以使用最小堆实现。
初始时堆为空。首先将最小的丑数 11 加入堆。
每次取出堆顶元素 x,则 xx 是堆中最小的丑数,由于 2x, 3x, 5x也是丑数,因此将 2x, 3x, 5x加入堆。
上述做法会导致堆中出现重复元素的情况。为了避免重复元素,可以使用哈希集合去重,避免相同元素多次加入堆。
在排除重复元素的情况下,第 n 次从最小堆中取出的元素即为第 n 个丑数。
复杂度分析
时间复杂度:O(nlogn)。得到第 nn 个丑数需要进行 n 次循环,每次循环都要从最小堆中取出1 个元素以及向最小堆中加入最多 3 个元素,因此每次循环的时间复杂度是 O(log n+log 3n)=O(log n),总时间复杂度是 O(nlogn)。
空间复杂度:O(n)。空间复杂度主要取决于最小堆和哈希集合的大小,最小堆和哈希集合的大小都不会超过 3n。
#define HASH_FIND_LONG(head, findint, out) HASH_FIND(hh, head, findint, sizeof(long), out)
#define HASH_ADD_LONG(head, intfield, add) HASH_ADD(hh, head, intfield, sizeof(long), add)
struct HashTable {
long key;
UT_hash_handle hh;
};
void insert(struct HashTable** hashTable, long ikey) {
struct HashTable* tmp;
HASH_FIND_LONG(*hashTable, &ikey, tmp);
if (tmp == NULL) {
tmp = malloc(sizeof(struct HashTable));
tmp->key = ikey;
HASH_ADD_LONG(*hashTable, key, tmp);
}
}
bool count(struct HashTable** hashTable, long ikey) {
struct HashTable* tmp;
HASH_FIND_LONG(*hashTable, &ikey, tmp);
return tmp == NULL;
}
struct Heap {
long* heap;
int heapSize;
bool (*cmp)(long, long);
};
void init(struct Heap* obj, int n, bool (*cmp)(long, long)) {
obj->heap = malloc(sizeof(long) * (n + 1));
obj->heapSize = 0;
obj->cmp = cmp;
}
bool cmp1(long a, long b) {
return a > b;
}
void swap(long* a, long* b) {
long tmp = *a;
*a = *b, *b = tmp;
}
void push(struct Heap* obj, long x) {
int p = ++(obj->heapSize), q = p >> 1;
obj->heap[p] = x;
while (q) {
if (!obj->cmp(obj->heap[q], obj->heap[p])) {
break;
}
swap(&(obj->heap[q]), &(obj->heap[p]));
p = q, q = p >> 1;
}
}
void pop(struct Heap* obj) {
swap(&(obj->heap[1]), &(obj->heap[(obj->heapSize)--]));
int p = 1, q = p << 1;
while (q <= obj->heapSize) {
if (q + 1 <= obj->heapSize) {
if (obj->cmp(obj->heap[q], obj->heap[q + 1])) {
q++;
}
}
if (!obj->cmp(obj->heap[p], obj->heap[q])) {
break;
}
swap(&(obj->heap[q]), &(obj->heap[p]));
p = q, q = p << 1;
}
}
long top(struct Heap* obj) {
return obj->heap[1];
}
bool empty(struct Heap* obj) {
return obj->heapSize == 0;
}
int nthUglyNumber(int n) {
int factors[3] = {2, 3, 5};
struct HashTable* hashTable = NULL;
insert(&hashTable, 1);
struct Heap* heap = malloc(sizeof(struct Heap));
init(heap, n * 3, cmp1);
push(heap, 1);
int ugly = 0;
for (int i = 0; i < n; i++) {
long curr = top(heap);
pop(heap);
ugly = (int)curr;
for (int i = 0; i < 3; i++) {
long next = curr * factors[i];
if (count(&hashTable, next)) {
insert(&hashTable, next);
push(heap, next);
}
}
}
return ugly;
}
3.动态规划
定义数组dp, 其中dp[i]表示第i个丑数,由于最小的丑数为1,所以dp[1]=1, 如何得到其余的丑数呢?定义三个指针 p2,p3,p5 ,表示下一个丑数是当前指针指向的丑数乘以对应的质因数。初始时,三个指针的值都是 1。
当2<=i<=n时,令dp[i]=min(dp[p2]*2,dp[p3]*3,dp[p5]*5), 之后比较dp[i]和dp[p2]*2,dp[p3]*3,
dp[p5]*5是否相等,如果相等,则对应的指针加1。
int min(int a,int b)
{
return (a<b)?a:b;
}
int nthUglyNumber(int n)
{
int dp[n+1];
dp[1]=1;
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]=min(min(num2,num3),num5);
if(dp[i]==num2)p2++;
if(dp[i]==num3)p3++;
if(dp[i]==num5)p5++;
}
return dp[n];
}
复杂度分析
时间复杂度:O(n)。需要计算数组 dp[n] 中的 n 个元素,每个元素的计算都可以在 O(1) 的时间内完成。
空间复杂度:O(n)。空间复杂度主要取决于数组 dp 的大小。