Skip to content

Commit

Permalink
Update Last Stone Weight.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul96 authored Nov 28, 2022
1 parent 63cb868 commit 90c41a5
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions Heap/Last Stone Weight.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
class Solution {
public:
int lastStoneWeight(vector<int>& s) {
priority_queue<int> a;
int w=0;
for(int i=0;i<s.size();i++){
a.push(s[i]);
int lastStoneWeight(vector<int>& nums) {
priority_queue<int> pq;
for(int i=0;i<nums.size();i++){
pq.push(nums[i]);
}
int w1=0,w2=0;
while(!a.empty()){
if(!a.empty()){
w1=a.top();
a.pop();
if(a.empty()){
return(w1);
}
else{
w2=a.top();a.pop();
if(w1!=w2){
a.push(max(w1,w2)-min(w1,w2));
}
}

if(nums.size()==1){
return nums[0];
}
while(!pq.empty() && pq.size()!=1){
int y = pq.top(); pq.pop();
int x = pq.top(); pq.pop();
if(x!=y){
pq.push(y-x);
}

}


return(0);
return pq.empty() ? 0:pq.top();
}
};

0 comments on commit 90c41a5

Please sign in to comment.