leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0313.cpp (640B)
0 class Solution { 1 public: 2 int nthSuperUglyNumber(int n, const vector<int> &primes) const { 3 using type_t = tuple<long long, int, int>; 4 priority_queue<type_t, vector<type_t>, greater<>> pq; 5 6 for (int i = 0; i < size(primes); i++) { 7 pq.emplace(primes[i], primes[i], 0); 8 } 9 10 static int nums[100002] = {1, 0}; 11 for (int i = 1; i < n;) { 12 const auto [num, prime, idx] = pq.top(); 13 pq.pop(); 14 if (num != nums[i - 1]) nums[i++] = num; 15 pq.emplace(1ll * prime * nums[idx + 1], prime, idx + 1); 16 } 17 18 return nums[n - 1]; 19 } 20 };