1 题目
1046. 最后一块石头的重量
2 代码实现
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
//默认创建的是最大堆
//priority_queue<int, vector<int>, less<int>> maxHeap;
//priority_queue<int, vector<int>, greater<int>> minHeap;
priority_queue<int> maxHeap(stones.begin(), stones.end());
while (maxHeap.size() > 1)
{
int firstMax = maxHeap.top(); maxHeap.pop();
int secondMax = maxHeap.top(); maxHeap.pop();
if (firstMax > secondMax)
maxHeap.push(firstMax-secondMax);
}
return maxHeap.empty() ? 0 : maxHeap.top();
}
};