Super Ugly Number

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

A super ugly number is a positive integer whose prime factors are in the array primes.

Given an integer n and a sorted array of distinct primes primes, return the nth super ugly number. The 1st super ugly number is always 1.

Examples

Example 1:

Input: n = 12, primes = [2,7,13,19]

Output: 32

Explanation: The super ugly numbers are [1,2,4,7,8,13,14,16,19,26,28,32]. The 12th is 32.

Example 2:

Input: n = 1, primes = [2,3,5]

Output: 1

Explanation: The 1st super ugly number is always 1.

Example 3:

Input: n = 10, primes = [2,3,5]

Output: 12

Explanation: The super ugly numbers are [1,2,3,4,5,6,8,9,10,12]. The 10th is 12.

Constraints

  • 1 <= n <= 10^6
  • 1 <= primes.length <= 15
  • 2 <= primes[i] <= 100
  • primes[i] is guaranteed to be prime.
  • All values of primes are unique and sorted in ascending order.
Source: K-way Merge pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle