55. Last Stone Weight
20 Jul 2020 | Daily Algorithms
priority_queue
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int> pq(begin(stones),end(stones)); // 즉 오름차순으로 정렬(112478)
while(pq.size()>1)
{
int x = pq.top();
pq.pop();
int y = pq.top();
pq.pop();
if(x>y)
pq.push(x-y);
}
return pq.empty() ? 0 : pq.top();
}
};
priority_queue
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int> pq(begin(stones),end(stones)); // 즉 오름차순으로 정렬(112478)
while(pq.size()>1)
{
int x = pq.top();
pq.pop();
int y = pq.top();
pq.pop();
if(x>y)
pq.push(x-y);
}
return pq.empty() ? 0 : pq.top();
}
};
Comments