136ms c++


  • 0
    B
    class Solution {
    public:
        int nthSuperUglyNumber(int n, vector<int>& primes)
        {
            vector<long long int> cnt(primes.size(),0);
            vector<long long int> result(n,0);
            result[0]=1;
            for(int i=1;i<n;i++)
            {
                long long int curVal=LONG_LONG_MAX;
                for(int j=0;j<primes.size();j++)
                    curVal=min(curVal,primes[j]*result[cnt[j]]);
                result[i]=curVal;
                for(int j=0;j<primes.size();j++)
                    if (primes[j]*result[cnt[j]]==curVal)
                        cnt[j]++;
            }
            return result[n-1];
        }
    };
    

Log in to reply
 

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