Leetcode 264 Solution

This article provides solution to leetcode question 264 (ugly-number-ii)

https://leetcode.com/problems/ugly-number-ii

Solution

class Solution {
public:
    int nthUglyNumber(int n) {
        set<int64_t> s;

        s.insert(1);

        for (int i = 1; i < n; i++)
        {
            int64_t curr = *s.begin();

            s.insert(curr * 2);
            s.insert(curr * 3);
            s.insert(curr * 5);

            s.erase(s.begin());
        }

        return (int)*s.begin();
    }
};