解题思路:
与Leetcode 264. 丑数 II的解题思路一样,均使用最小堆来存储丑数,第i次更新最小堆时,得到第i大的丑数。
可结合Leetcode 264. 丑数 II的解题思路理解:https://blog.csdn.net/gjh13/article/details/90247437
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
priority_queue<int, vector<int>, greater<int>> pq;
pq.push(1);
int last = 0;
int i = 0;
while(i < n){
if(last == pq.top()) pq.pop();
else{
long tmp = pq.top(); //为了防止tmp * primes[j] 超出int的数值范围,将tmp定义为long
for(int j = 0; j < primes.size(); j++){
if(tmp * primes[j] < INT_MAX) pq.push(tmp * primes[j]);
}
i++;
last = tmp;
pq.pop();
}
}
return last;
}
};