一.题目解析

每次拿两个最大的值,相等就消掉,不相等就将abs(x-y)放回数组里面
算法解析:堆->大根堆
每次我们都需要找两个最大的值,我们可以使用大根堆来快速找到最大值

二.代码编写:
cpp
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int>heap;//大根堆
for(auto x:stones)heap.push(x);
while(heap.size()>1)
{
int x=heap.top();heap.pop();
int y=heap.top();heap.pop();
if(x>y)heap.push(x-y);//相等会碎掉,不放回
}
return heap.empty()?0:heap.top();//堆是空就需要返回零
}
};