class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int> q;
for(auto x:stones){
q.push(x);
}
while(q.size()>=2){
int y=q.top();
q.pop();
int x=q.top();
q.pop();
if(x!=y){
q.push(y-x);
}
}
if(q.empty()) return 0;
return q.top();
}
};
Leetcode 1046. 最后一块石头的重量 最大堆/优先队列
最新推荐文章于 2025-08-26 19:31:33 发布