Java solution based on ugly number (98%)


  • 0
    C
    public class Solution {
    public int nthSuperUglyNumber(int n, int[] primes) {
    int[] count=new int[primes.length];
    int[] next=new int[primes.length];
    int[] ugly=new int[n];
    Arrays.fill(next,1);
    
    for(int i=0;i<n;i++)
    {
        int tem=Integer.MAX_VALUE;
        for(int j:next)
        tem=Math.min(tem,j);
        
        ugly[i]=tem;
        
        for(int j=0;j<primes.length;j++)
        {
            if(tem==next[j])
            next[j]=primes[j]*(ugly[count[j]++]);
        }
    }
    return ugly[n-1];
    }
    

    }


Log in to reply
 

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