1046 最后一块石头的重量

水题,思路很直观。

class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        priority_queue<int, vector<int>> pq; // 默认大顶堆
        for(auto i : stones) pq.push(i);
        while(pq.size() > 1)
        {
            int x = pq.top();
            pq.pop();
            int y = pq.top();
            pq.pop();
            if(x > y) pq.push(x - y);
        }
        if(pq.size()) return pq.top();
        else return 0;
    }
};
发布了21 篇原创文章 · 获赞 32 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/include_not_found_/article/details/104862531