C# solution


  • 0
    K
    public class Solution {
        public int NthSuperUglyNumber(int n, int[] primes) {
            if (n == 1) return 1;
            int[] ugly = new int[n];
            ugly[0] = 1;
            int len = primes.Length;
            int[] values = new int[len];
            for (int i=0;i<len;i++)
                values[i] = 0;
            for (int j=1;j<n;j++) {
                int min = Int32.MaxValue;
                for (int i=0;i<len;i++) {
                    int val = ugly[values[i]]*primes[i];
                    if (val < min)  min = val;
                }
                for (int i=0;i<len;i++)
                    if (min == ugly[values[i]]*primes[i]) values[i]++;
                ugly[j]=min;
            }
            return ugly[n-1];
        }
    }
    

Log in to reply
 

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