-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_STO_0049_nthUglyNumber.cc
83 lines (77 loc) · 1.38 KB
/
Problem_STO_0049_nthUglyNumber.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <functional>
#include <iostream>
#include <queue>
#include <unordered_set>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 模拟,时间复杂度较大
int nthUglyNumber(int n)
{
vector<int> uglys = {2, 3, 5};
priority_queue<long, vector<long>, std::greater<long>> que;
unordered_set<long> seen;
que.push(1L);
seen.emplace(1L);
int ans = 1;
while (n-- && !que.empty())
{
long cur = que.top();
que.pop();
ans = static_cast<int>(cur);
for (auto &ugly : uglys)
{
int next = cur * ugly;
if (seen.find(next) == seen.end())
{
que.push(next);
seen.emplace(next);
}
}
}
return ans;
}
int dp(int n)
{
vector<int> dp(n + 1);
dp[1] = 1;
int p2 = 1;
int p3 = 1;
int p5 = 1;
for (int i = 2; i <= n; i++)
{
int num2 = dp[p2] * 2;
int num3 = dp[p3] * 3;
int num5 = dp[p5] * 5;
dp[i] = std::min(std::min(num2, num3), num5);
if (dp[i] == num2)
{
p2++;
}
if (dp[i] == num3)
{
p3++;
}
if (dp[i] == num5)
{
p5++;
}
}
return dp[n];
}
};
void test()
{
Solution s;
EXPECT_EQ_INT(12, s.nthUglyNumber(10));
EXPECT_EQ_INT(12, s.dp(10));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}