Cpp O(N logK) solution
Here\'s the explanation of the basic idea. \nWe have K arrays. The ith array is generated by primes[i] x (ans[0], ans[1], ans[2], .etc). And we can treat this p
Here\'s the [explanation of the basic idea](https://www.geeksforgeeks.org/ugly-numbers/). \nWe have K arrays. The ith array is generated by primes[i] x (ans[0], ans[1], ans[2], .etc). And we can treat this problem as to find the nth value of k sorted arrays. So I use a min heap(priority_queue) to store the first value of the k arrays. And if we find the lowest value(top of the heap) on ith array, we push its next element into the heap. Be careful of the duplicates.\n```\nclass Solution {\npublic:\n    int nthSuperUglyNumber(int n, vector<int>& primes) {\n        priority_queue<Num, vector<Num>, myCompare> pq;\n        vector<int> ans = {1};\n        for(int i : primes)\n            pq.push(Num(i, 0, i));\n        for(int i = 1; i < n; ++i) {\n            int tmp = pq.top().next;\n            ans.push_back(tmp);\n\t\t\t//duplicate\n            while(pq.top().next == tmp) {\n                Num n = pq.top();\n                pq.pop();\n                pq.push(Num(n.prime, n.index + 1, n.prime * ans[n.index + 1]));\n            }\n        }\n        return ans.back();\n            \n    }\nprivate:\n    struct Num {\n        int prime;\n        int index;\n        int next;\n        \n        Num(int p, int i, int n): prime(p), index(i), next(n) {\n            \n        }\n    };\n    struct myCompare {\n        bool operator()(Num a, Num b) {\n            return a.next > b.next;\n        }\n    };\n};\n```\nComplexity:\n* Time: O(n * logk)\n* Space: O(n + k)
['C++']