A super ugly number is a positive integer whose only prime factors are found in a given array of primes. Think of it as building numbers using only specific "building blocks" (prime factors).
Given an integer n and an array of integers primes, your task is to find the n-th super ugly number. The sequence starts with 1 (which has no prime factors), and each subsequent number can only be formed by multiplying previous super ugly numbers with the given primes.
Example: If primes = [2, 7, 13, 19], the sequence begins: 1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32...
Your goal is to efficiently generate this sequence and return the n-th number, which is guaranteed to fit in a 32-bit signed integer.
Input & Output
Visualization
Time & Space Complexity
For each of n positions, we check k primes to find minimum candidate
O(n) for result array and O(k) for pointers array where k is number of primes
Constraints
- 1 ≤ n ≤ 106
- 1 ≤ primes.length ≤ 100
- 2 ≤ primes[i] ≤ 1000
- primes[i] is guaranteed to be a prime number
- All the values of primes are unique and sorted in ascending order