All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0301_0400.s0313_super_ugly_number.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0301_0400.s0313_super_ugly_number;

// #Medium #Array #Dynamic_Programming #Hash_Table #Math #Heap_Priority_Queue

public class Solution {
    public int nthSuperUglyNumber(int n, int[] primes) {
        if (primes.length == 0) {
            return 1;
        }
        int[] uglies = new int[n];
        uglies[0] = 1;
        int[] uglyToMultiplyBy = new int[primes.length];

        for (int i = 1; i < n; i++) {
            int min = primes[0] * uglies[uglyToMultiplyBy[0]];
            int minIdx = 0;
            for (int j = 1; j < primes.length; j++) {
                int currNum = primes[j] * uglies[uglyToMultiplyBy[j]];
                if (currNum < min) {
                    min = currNum;
                    minIdx = j;
                } else if (currNum == min) {
                    uglyToMultiplyBy[j]++;
                }
            }
            uglyToMultiplyBy[minIdx]++;
            uglies[i] = min;
        }
        return uglies[n - 1];
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy