Skip to content

Commit

Permalink
Create 2558. Take Gifts From the Richest Pile1
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Dec 12, 2024
1 parent 45cc82d commit 9a6da5b
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 2558. Take Gifts From the Richest Pile1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution1 {
public:
long long pickGifts(vector<int>& gifts, int k) {
long long ans=0;
for(int i=0;i<k;i++){
int m=0;
for(int j=1;j<gifts.size();j++) if(gifts[j]>gifts[m]) m=j;
int a=gifts[m];
// cout<<a<<endl;
a=sqrt(a);
// cout<<a<<endl;
gifts[m]=a;
}
for(int i : gifts){
ans+=i;
}
return ans;
}
};

class Solution {
public:
long long pickGifts(vector<int>& gifts, int k) {
long long ans=0;
priority_queue<int> vp;
for(int i : gifts) vp.push(i);
for(int i=0;i<k;i++){
int a=vp.top();
vp.pop();
// cout<<a<<endl;
a=sqrt(a);
// cout<<a<<endl;
vp.push(a);
}
while(!vp.empty()){
ans+=vp.top();
vp.pop();
}
return ans;
}
};

0 comments on commit 9a6da5b

Please sign in to comment.