LeetCode 313: Super Ugly Number Solution

Master LeetCode problem 313 (Super Ugly Number), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

313. Super Ugly Number

Problem Explanation

Explanation:

To solve this problem, we can use a similar approach to the ugly number problem by maintaining pointers for each prime factor in the primes array. We will generate super ugly numbers by multiplying each prime factor with the current super ugly number and selecting the minimum among them.

Algorithm:

  1. Initialize an array ugly to store the super ugly numbers.
  2. Initialize an array pointers to keep track of the current index for each prime factor in the primes array.
  3. Initialize an array nextMultiples to store the next multiple for each prime factor.
  4. Populate the ugly array with 1 as the first super ugly number.
  5. Iterate from 1 to n (exclusive) to generate the next super ugly numbers:
    • Calculate the next super ugly number by multiplying each prime factor with the current super ugly number and selecting the minimum.
    • Update the respective pointers and next multiples for each prime factor.
    • Add the new super ugly number to the ugly array.
  6. Return the nth super ugly number.

Time Complexity:

The time complexity of this solution is O(n * k), where n is the input n and k is the number of prime factors in the primes array.

Space Complexity:

The space complexity of this solution is O(n + k), where n is the input n and k is the number of prime factors in the primes array.

:

Solution Code

class Solution {
    public int nthSuperUglyNumber(int n, int[] primes) {
        int[] ugly = new int[n];
        int[] pointers = new int[primes.length];
        int[] nextMultiples = new int[primes.length];
        
        ugly[0] = 1;
        
        for (int i = 1; i < n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 0; j < primes.length; j++) {
                min = Math.min(min, primes[j] * ugly[pointers[j]]);
            }
            ugly[i] = min;
            
            for (int j = 0; j < primes.length; j++) {
                if (min == primes[j] * ugly[pointers[j]]) {
                    pointers[j]++;
                }
            }
        }
        
        return ugly[n - 1];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 313 (Super Ugly Number)?

This page provides optimized solutions for LeetCode problem 313 (Super Ugly Number) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 313 (Super Ugly Number)?

The time complexity for LeetCode 313 (Super Ugly Number) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 313 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 313 in Java, C++, or Python.

Back to LeetCode Solutions