Create Time: 30th April 2024
Title: "[[Priority-Queue]]"
status: UNFINISHED
Author:
- AllenYGY
tags:
- DataStructure
- Cpp
- NOTE
created: 2024-04-30T19:23
updated: 2024-04-30T19:39
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int> q;
for(auto v:stones){
q.push(v);
}
while(q.size()>1){
int x=q.top();
q.pop();
int y=q.top();
q.pop();
if(x!=y)
q.push(x-y);
}
if(q.size()==0)
return 0;
int ans=q.top();
return ans;
}
};
class Solution {
public:
long long pickGifts(vector<int>& gifts, int k) {
priority_queue<long long > q;
for(auto v:gifts){
q.push(v);
}
while(k--){
long long top=q.top();
q.pop();
top=int(sqrt(top));
q.push(top);
}
long long ans=0;
while(q.size()!=0){
ans+=q.top();
q.pop();
}
return ans;
}
};