Sharing my 116ms C++ solution


  • -1
    T
    class Solution {
    public:
        int nthSuperUglyNumber(int n, vector<int>& primes) {
            int N = primes.size();
            vector<int> result;
            result.push_back(1);
            vector<int> count(N, 0);
            
            while(result.size()<n)
            {
                int temp = INT_MAX;
                int i;
                for(i=0; i<N; i++)
                    temp = min(temp, primes[i]*result[count[i]]);
                result.push_back(temp);
                for(i=0; i<N; i++)
                    if(temp == primes[i]*result[count[i]])
                    {
                        count[i]++;
                    }
            }
            
            return result.back();
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.