🔗 https://leetcode.com/problems/take-gifts-from-the-richest-pile
题目
- 给一个数组,每次选最大的数,替换为其 sqrt
- 进行过 k 次操作后,返回数组的 sum
思路
- 大根堆,优先队列
代码
class Solution {
public:
long long pickGifts(vector<int>& gifts, int k) {
std::priority_queue<long long, std::vector<long long>> hp;
for (auto item : gifts) {
hp.push(item);
}
while (k--) {
long long item = hp.top(); hp.pop();
item = sqrt(item);
hp.push(item);
}
long long ans = 0;
while (!hp.empty()) {
ans += hp.top(); hp.pop();
}
return ans;
}
};