> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Super Ugly Number Python Solution with Tests

> Tested Python solution for LeetCode 313 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 313, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/super-ugly-number/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 313   # by problem number
lcpy gen -s super_ugly_number   # by problem name
```

## Problem

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.

### Examples

```
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].
```

```
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 \<= 10^5
* 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.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/super_ugly_number/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/super_ugly_number/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * k) where k = len(primes)
    # Space: O(n + k)
    def nth_super_ugly_number(self, n: int, primes: list[int]) -> int:
        ugly = [0] * n
        ugly[0] = 1
        idx = [0] * len(primes)
        candidates = list(primes)
        for i in range(1, n):
            nxt = min(candidates)
            ugly[i] = nxt
            for j, prime in enumerate(primes):
                if candidates[j] == nxt:
                    idx[j] += 1
                    candidates[j] = ugly[idx[j]] * prime
        return ugly[n - 1]
```

## Complexity

| Time | Space |
| - | - |
| O(n \* k) where k = len(primes) | O(n + k) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.