# 1046.Last Stone Weight

**1046.Last Stone Weight**

难度:Easy

> 有一堆石头，每块石头的重量都是正整数。

每一回合，从中选出两块最重的石头，然后将它们一起粉碎。假设石头的重量分别为 x 和 y，且 x <= y。那么粉碎的可能结果如下：

如果 x == y，那么两块石头都会被完全粉碎； 如果 x != y，那么重量为 x 的石头将会完全粉碎，而重量为 y 的石头新重量为 y-x。 最后，最多只会剩下一块石头。返回此石头的重量。如果没有石头剩下，就返回 0。

提示：

1 <= stones.length <= 30 1 <= stones\[i] <= 1000

可以使用最大堆解决。

```
class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        if(stones.size()==1) return stones[0];
        priority_queue<int> tmp(stones.begin(),stones.end());
        while(tmp.size()>1)
        {
            int max=tmp.top();
            tmp.pop();
            int sec=tmp.top();
            tmp.pop();
            int res=max-sec;
            tmp.push(res);
        }
        return tmp.top();
    }
};
```

> 执行用时 : 8 ms, 在Last Stone Weight的C++提交中击败了42.50% 的用户\
> 内存消耗 : 8.5 MB, 在Last Stone Weight的C++提交中击败了100.00% 的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/1046.last_stone_weight.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
