当前位置: 代码迷 >> 综合 >> leetcode 1046. Last Stone Weight
  详细解决方案

leetcode 1046. Last Stone Weight

热度:42   发布时间:2024-01-16 17:53:09.0

leetcode 1046. Last Stone Weight

思路:用优先队列简单模拟,挑最大的两块撞一下。

代码:

class Solution {
public:int lastStoneWeight(vector<int>& stones) {priority_queue<int> q;for (auto a:stones)q.push(a);while (!q.empty()){if (q.size() == 1)return q.top();else{auto max1 = q.top();q.pop();auto max2 = q.top();q.pop();auto left = abs(max2 - max1);if (left != 0)q.push(left);}}return 0;}
};

 

  相关解决方案