A super ugly number is a positive integer whose prime factors are in the array primes.
Given an integer n and an array of integers primes, return the nth super ugly number.
The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: n = 12, primes = [2,7,13,19] Output: 32 Explanation: [1,2,4,7,8,13,14,16,19,26,28,32] is the sequence of the first 12 super ugly numbers given primes = [2,7,13,19].
Example 2:
Input: n = 1, primes = [2,3,5] Output: 1 Explanation: 1 has no prime factors, therefore all of its prime factors are in the array primes = [2,3,5].
Constraints:
1 <= n <= 1051 <= primes.length <= 1002 <= primes[i] <= 1000primes[i] is guaranteed to be a prime number.primes are unique and sorted in ascending order.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find super ugly numbers using brute force, we essentially try generating every possible number that could be one. We keep generating numbers and checking if they fit our definition of a super ugly number, discarding the ones that don't.
Here's how the algorithm would work step-by-step:
def super_ugly_number_brute_force(number_of_ugly_numbers, primes):
super_ugly_numbers = [1]
while len(super_ugly_numbers) < number_of_ugly_numbers:
potential_ugly_numbers = []
for ugly_number in super_ugly_numbers:
for prime_number in primes:
new_number = ugly_number * prime_number
# We add a new number to the pool
potential_ugly_numbers.append(new_number)
next_ugly_number = min(potential_ugly_numbers)
# Check if the number is actually super ugly
is_super_ugly = True
temp_number = next_ugly_number
for prime_number in primes:
while temp_number % prime_number == 0:
temp_number //= prime_number
if temp_number != 1:
is_super_ugly = False
# Append only if it is truly super ugly
if is_super_ugly and next_ugly_number > super_ugly_numbers[-1]:
super_ugly_numbers.append(next_ugly_number)
#Eliminate duplicates after adding a new super ugly number
super_ugly_numbers = sorted(list(set(super_ugly_numbers)))
#If we exceeded the number needed we exit the loop
if len(super_ugly_numbers) >= number_of_ugly_numbers:
break
return super_ugly_numbers[number_of_ugly_numbers - 1]Instead of exhaustively checking every single number, we build the sequence of Super Ugly Numbers from the ground up. We keep track of the next potential multiple of each prime factor, always selecting the smallest one to extend our sequence and advancing the corresponding prime factor's multiple.
Here's how the algorithm would work step-by-step:
def super_ugly_number(number, primes):
ugly_numbers = [1]
prime_indices = [0] * len(primes)
for _ in range(1, number):
next_ugly_numbers = [primes[i] * ugly_numbers[prime_indices[i]] for i in range(len(primes))]
# Find the minimum potential ugly number.
next_ugly = min(next_ugly_numbers)
ugly_numbers.append(next_ugly)
# Advance prime indices for multiples == min.
for i in range(len(primes)):
if next_ugly_numbers[i] == next_ugly:
prime_indices[i] += 1
return ugly_numbers[-1]| Case | How to Handle |
|---|---|
| Empty primes array | If primes is empty, return an empty list or a list containing only 1, depending on problem requirements. |
| n is 0 or negative | If n is 0, return an empty list; if n is negative, throw an IllegalArgumentException or return an empty list, as super ugly numbers are not defined for non-positive n. |
| n is 1 | Return a list containing only 1 since the first super ugly number is always 1. |
| primes array contains duplicates | The algorithm should still work correctly as duplicates won't affect the generation of super ugly numbers if the algorithm already avoids using the same index twice. |
| primes array contains 1 | The algorithm should handle this correctly; if 1 is in primes, the loop will iterate on 1, multiplying by 1 repeatedly but not adding to the result if already present. |
| Integer overflow when multiplying prime numbers | Use long data type to prevent integer overflow during multiplication of existing super ugly numbers and prime numbers. |
| Large n value causing memory issues | The algorithm can be optimized with generators in languages like Python or lazy evaluation to minimize memory usage for very large n. |
| primes array contains very large prime numbers | Using long data type will handle them, however execution time can be affected by repeated large number multiplications. |