Leetcode 313 Solution

This article provides solution to leetcode question 313 (super-ugly-number)

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

Solution

class Solution {
public:
    int nthSuperUglyNumber(int n, vector<int>& primes) {
        set<int64_t> a;

        a.insert(1);

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

            for (int j = 0; j < primes.size(); j++)
            {
                if (curr * primes[j] > INT_MAX)
                    break;

                a.insert(curr * primes[j]);
            }

            a.erase(a.begin());
        }

        return *a.begin();
    }
};