<p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>

<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 1690</code></li>
</ul>

#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native")
using namespace std;
#define all(x) x.begin(), x.end()


using ull = unsigned long long;
using ll = long long;

inline int gcd(int a,int b) { if (b==0) return a; return gcd(b, a%b); }
inline long long gcd(long long a,long long b) { if (b==0) return a; return gcd(b, a%b); }
inline int lcm(int a,int b) { return a/gcd(a,b)*b; } 
inline long long lcm(long long a,long long b) { return a/gcd(a,b)*b; }
inline long long nth_prime(long long a) { a++;if(a <= 6) return (vector<long long>{2,3,5,7,11,13,17})[a]; long double lg = log((long double) a); return (long long) floor(a * (lg + log(lg))); }
inline long long mod_exp(long long base, long long exp, long long modd) { unsigned long long ans = 1; base %= modd; while(exp > 0) { if(exp%2==1) ans = (base*ans)%modd; exp /= 2; base = (base*base)%modd; } return ans; }
inline string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }
inline string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }

vector<int> uglyNumbers;

bool init() {
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);

    priority_queue<ll, vector<ll>, greater<ll>> minHeap;
    minHeap.push(1);
    uglyNumbers.reserve(1691);
    for(int n = 0; n <= 1690; n++) {
        ll num;
        do {
            num = minHeap.top();
            minHeap.pop();
        }while(n > 0 && uglyNumbers[n-1] == num);
        minHeap.push(2ll*num);
        minHeap.push(3ll*num);
        minHeap.push(5ll*num);
        uglyNumbers.push_back((int)num);
    }

    return true;
}
bool y4555123 = init();

class Solution {
public:
    int nthUglyNumber(int n) {
        return uglyNumbers[n-1];
    }
};
8390