-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path264.cpp
More file actions
33 lines (33 loc) · 676 Bytes
/
264.cpp
File metadata and controls
33 lines (33 loc) · 676 Bytes
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
// dp.cpp
class Solution {
public:
int nthUglyNumber(int n) {
int a = 0, b = 0, c = 0;
vector<int> nums(n, 1);
for (int i = 1; i < n; ++i) {
nums[i] = min(nums[a] * 2, min(nums[b] * 3, nums[c] * 5));
if (nums[i] == nums[a] * 2)
++a;
if (nums[i] == nums[b] * 3)
++b;
if (nums[i] == nums[c] * 5)
++c;
}
return nums[n - 1];
}
};
// set.cpp
class Solution2 {
public:
int nthUglyNumber(int n) {
set<long long int> s;
s.insert(1);
auto it = s.begin();
for (int i = 1; i < n; ++i, ++it) {
s.insert(*it * 2);
s.insert(*it * 3);
s.insert(*it * 5);
}
return *it;
}
};