Skip to content

Commit e98bda5

Browse files
committed
feat(LeetCode): Solve leetcode/leetcoding_challenge/2024/aug2024/week3/ugly_number_ii.cpp
1 parent 44e9616 commit e98bda5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <algorithm>
2+
#include <vector>
3+
#include <queue>
4+
#include <set>
5+
#include <limits>
6+
#include <map>
7+
#include <unordered_set>
8+
#include <unordered_map>
9+
#include <iterator>
10+
#include <sstream>
11+
#include <iostream> // includes cin to read from stdin and cout to write to stdout
12+
using namespace std; // since cin and cout are both in namespace std, this saves some text
13+
14+
class Solution {
15+
public:
16+
int nthUglyNumber(int n) {
17+
if (n == 1) {
18+
return 1;
19+
}
20+
21+
unordered_set<long> appeared;
22+
priority_queue<long, vector<long>, greater<long>> pq;
23+
pq.push(1);
24+
25+
vector<long> FACTORS{2, 3, 5};
26+
27+
long num = 0;
28+
while (n > 0) {
29+
num = pq.top();
30+
pq.pop();
31+
32+
if (appeared.count(num) > 0) {
33+
continue;
34+
}
35+
appeared.insert(num);
36+
37+
n--;
38+
39+
for (auto f: FACTORS) {
40+
pq.push(num * f);
41+
}
42+
}
43+
return num;
44+
}
45+
};

0 commit comments

Comments
 (0)