We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a935f68 commit 0d7abdaCopy full SHA for 0d7abda
scripts/algorithms/U/Ugly Number II/Ugly Number II.cpp
@@ -1,29 +1,18 @@
1
-// Runtime: 4 ms (Top 94.55%) | Memory: 6 MB (Top 90.93%)
+// Runtime: 113 ms (Top 11.61%) | Memory: 30.00 MB (Top 16.43%)
2
+
3
class Solution {
4
public:
5
int nthUglyNumber(int n) {
-
6
- int dp[n+1];
7
- dp[1] = 1;
8
9
- int p2 = 1, p3 = 1, p5 = 1;
10
- int t;
11
12
- for(int i = 2; i < n+1; i++){
13
- t = min(dp[p2]*2, min(dp[p3]*3, dp[p5]*5));
14
- dp[i] = t;
15
16
- if(dp[i] == dp[p2]*2){
17
- p2++;
18
- }
19
- if(dp[i] == dp[p3]*3){
20
- p3++;
21
22
- if(dp[i] == dp[p5]*5){
23
- p5++;
24
+ set<long> st;
+ st.insert(1);
+ long num = 1;
+ for(int i=0;i<n;i++){
+ num = *st.begin();
+ st.erase(num);
+ st.insert(num * 2);
+ st.insert(num * 3);
+ st.insert(num * 5);
25
}
26
- return dp[n];
27
+ return num;
28
29
-};
+};
0 commit comments