Sign in with Google

Google will share your name, email, and profile picture with DevExCode. See our privacy policy.

LeetCode 313: Super Ugly Number

LeetCode 313 Solution 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.

:

LeetCode 313 Solutions in Java, C++, Python

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];
    }
}

Interactive Code Editor for LeetCode 313

Improve Your LeetCode 313 Solution

Use the editor below to refine the provided solution for LeetCode 313. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems