Solution By Dare2Solve | Detailed Explanation | Clean Code
Explanation []\nauthorslog.com/blog/bWywGNyxhF\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n    struct Compare {\n        bool operator()(const vector<long>&
```Explanation []\nauthorslog.com/blog/bWywGNyxhF\n```\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    struct Compare {\n        bool operator()(const vector<long>& a, const vector<long>& b) {\n            return a[0] > b[0];\n        }\n    };\n\n    int nthSuperUglyNumber(int n, vector<int>& primes) {\n        priority_queue<vector<long>, vector<vector<long>>, Compare> minPQ;\n\n        // Initialize the priority queue with the first prime multiples\n        for (int prime : primes) {\n            minPQ.push({prime, 0, prime}); // {value, index in output, prime factor}\n        }\n\n        vector<long> output(1, 1); // The first super ugly number is always 1\n\n        while (output.size() < n) {\n            vector<long> current = minPQ.top();\n            minPQ.pop();\n\n            long value = current[0], idx = current[1], prime = current[2];\n\n            // Avoid adding duplicate values to the output array\n            if (value != output.back()) {\n                output.push_back(value);\n            }\n\n            // Generate the next multiple of the current prime\n            minPQ.push({output[idx + 1] * prime, idx + 1, prime});\n        }\n\n        return output[n - 1];\n    }\n};\n\n```\n\n```python []\nclass Solution:\n    def nthSuperUglyNumber(self, n: int, primes: list[int]) -> int:\n        minPQ = []\n        \n        # Initialize the priority queue with the first prime multiples\n        for prime in primes:\n            heapq.heappush(minPQ, (prime, 0, prime))  # (value, index in output, prime factor)\n        \n        output = [1]  # The first super ugly number is always 1\n\n        while len(output) < n:\n            value, idx, prime = heapq.heappop(minPQ)\n            \n            # Avoid adding duplicate values to the output list\n            if value != output[-1]:\n                output.append(value)\n            \n            # Generate the next multiple of the current prime\n            heapq.heappush(minPQ, (output[idx + 1] * prime, idx + 1, prime))\n        \n        return output[n - 1]\n\n```\n\n```java []\nclass Solution {\n    public int nthSuperUglyNumber(int n, int[] primes) {\n        // Priority queue to get the smallest current number\n        PriorityQueue<long[]> minPQ = new PriorityQueue<>(Comparator.comparingLong(a -> a[0]));\n\n        // Initialize the priority queue with the first prime multiples\n        for (int prime : primes) {\n            minPQ.offer(new long[] {prime, 0, prime}); // [value, index in output, prime factor]\n        }\n\n        // Array to store the ugly numbers\n        long[] output = new long[n];\n        output[0] = 1; // The first super ugly number is always 1\n\n        // Populate the output array\n        for (int i = 1; i < n; i++) {\n            long[] current = minPQ.poll();\n            long value = current[0];\n            int idx = (int)current[1];\n            long prime = current[2];\n\n            // Avoid adding duplicate values to the output array\n            if (value != output[i - 1]) {\n                output[i] = value;\n            } else {\n                i--; // If duplicate, redo this index\n            }\n\n            // Generate the next multiple of the current prime\n            minPQ.offer(new long[] {output[idx + 1] * prime, idx + 1, prime});\n        }\n\n        return (int)output[n - 1];\n    }\n}\n```\n\n```javascript []\n/**\n * @param {number} n\n * @param {number[]} primes\n * @return {number}\n */\nvar nthSuperUglyNumber = function (n, primes) {\n    const minPQ = new PriorityQueue({ compare: (a, b) => a[0] - b[0] });\n\n    // Initialize the priority queue with the first prime multiples\n    for (const prime of primes) {\n        minPQ.enqueue([prime, 0, prime]); // [value, index in output, prime factor]\n    }\n\n    const output = [1]; // The first super ugly number is always 1\n\n    while (output.length < n) {\n        const [value, idx, prime] = minPQ.dequeue();\n        \n        // Avoid adding duplicate values to the output array\n        if (value !== output[output.length - 1]) {\n            output.push(value);\n        }\n        \n        // Generate the next multiple of the current prime\n        minPQ.enqueue([output[idx + 1] * prime, idx + 1, prime]);\n    }\n\n    return output[n - 1];\n};\n```
['Math', 'Dynamic Programming', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']