Very Simple and Clean JAVA code


  • 1
    F
    public int nthSuperUglyNumber(int n, int[] primes) {
        int[] a = new int[n];
        int k = primes.length;
        int[] p = new int[k];
        a[0] = 1;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < k; j++) {
                while (primes[j] * a[p[j]] <= a[i - 1]) {
                    p[j]++;
                }
            }
            a[i] = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                a[i] = Math.min(a[i], primes[j] * a[p[j]]);
            }
        }
        return a[n - 1];
    }

Log in to reply
 

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