Skip to content

Commit

Permalink
feat(problem-2558/cpp): solve by using std::priority_queue
Browse files Browse the repository at this point in the history
Signed-off-by: Alfi Maulana <[email protected]>
  • Loading branch information
threeal committed Dec 12, 2024
1 parent f0aea4c commit a79d031
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion old-problems/2558/solution.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
#include <cmath>
#include <queue>
#include <vector>

class Solution {
public:
long long pickGifts(std::vector<int>& gifts, int k) {
return gifts.size() * k;
std::priority_queue<int> richest{};
for (const auto gift : gifts) richest.push(gift);

while (k > 0) {
richest.push(std::sqrt(richest.top()));
richest.pop();
--k;
}

long long total{0};
while (!richest.empty()) {
total += richest.top();
richest.pop();
}

return total;
}
};

0 comments on commit a79d031

Please sign in to comment.