My super ugly code base on Ugly Number II


  • 0
    E
    int nthSuperUglyNumber(int n, vector<int>& primes) {
        int size = primes.size();
        vector<int> idx(size, 0);
        vector<int> f(size, 1);
        vector<int> dp(n);
        for(int i = 0; i < n; ++i) {
            dp[i] = *min_element(f.begin(), f.end());
            for(int j = 0; j < size; ++j) {
                if(f[j] == dp[i]) f[j] = primes[j] * dp[idx[j]++];
            }
        }
    
        return dp[n-1];
    }

Log in to reply
 

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