<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>

class Solution {
public:
    int nthUglyNumber(int n) {
        // int count = 1;
        // priority_queue<int, vector<int>, greater<int>> pq;
        // unordered_set<int> nums_set;
        // pq.push(1);
        // int result = 1;
        // while (nums_set.size() < n) {
        //     result = pq.top();
        //     pq.pop();
        //     nums_set.insert(result);
        //     if (!nums_set.count(result * 2)) {
        //         pq.push(result * 2);
        //     }
        //     if (!nums_set.count(result * 3)) {
        //         pq.push(result * 3);
        //     }
        //     if (!nums_set.count(result * 5)) {
        //         pq.push(result * 5);
        //     }
        // }
        // return result;
        
        // int count = 1;
        int result = 1;
        priority_queue<int, vector<int>, greater<int>> pq_multiply_2;
        priority_queue<int, vector<int>, greater<int>> pq_multiply_3;
        priority_queue<int, vector<int>, greater<int>> pq_multiply_5;
        unordered_set<int> count_set;
        pq_multiply_2.push(1);
        pq_multiply_3.push(1);
        pq_multiply_5.push(1);
        count_set.insert(1);
        while (count_set.size() < n) {
            cout << "count: " << count_set.size() << ", result: " << result << endl;
            int num_multiplied_2 = pq_multiply_2.top() * 2;
            while (count_set.count(num_multiplied_2)) {
                pq_multiply_2.pop();
                num_multiplied_2 = pq_multiply_2.top() * 2;
            }
            int num_multiplied_3 = pq_multiply_3.top() * 3;
            while (count_set.count(num_multiplied_3)) {
                pq_multiply_3.pop();
                num_multiplied_3 = pq_multiply_3.top() * 3;
            }
            int num_multiplied_5 = pq_multiply_5.top() * 5;
            while (count_set.count(num_multiplied_5)) {
                pq_multiply_5.pop();
                num_multiplied_5 = pq_multiply_5.top() * 5;
            }
            if (num_multiplied_5 < num_multiplied_3 && num_multiplied_5 < num_multiplied_2) {
                result = num_multiplied_5;
                pq_multiply_5.pop();
                pq_multiply_2.push(num_multiplied_5);
                pq_multiply_3.push(num_multiplied_5);
                pq_multiply_5.push(num_multiplied_5);
            } else if (num_multiplied_3 < num_multiplied_2) {
                result = num_multiplied_3;
                pq_multiply_3.pop();
                pq_multiply_2.push(num_multiplied_3);
                pq_multiply_3.push(num_multiplied_3);
                pq_multiply_5.push(num_multiplied_3);
            } else {
                result = num_multiplied_2;
                pq_multiply_2.pop();
                pq_multiply_2.push(num_multiplied_2);
                pq_multiply_3.push(num_multiplied_2);
                pq_multiply_5.push(num_multiplied_2);
            }
            count_set.insert(result);
            // count++;
        }
        return result;
    }
};
29053