22ms Java solution


  • 0
    X

    '''
    public int nthSuperUglyNumber(int n, int[] primes) {
    int[] idx = new int[primes.length];
    int[] nums = new int[n];
    nums[0] = 1;
    int ctr = 1;
    while(ctr < n) {
    int min = Integer.MAX_VALUE;
    for(int i = 0; i < primes.length; i++) {
    int t = nums[idx[i]] * primes[i];
    if(t <= nums[ctr-1]) {
    idx[i]++;
    t = nums[idx[i]] * primes[i];
    }
    min = Math.min(min, t);
    }
    nums[ctr] = min;
    ctr++;
    }
    return nums[n-1];
    }
    '''


Log in to reply
 

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