题目
https://leetcode-cn.com/problems/last-stone-weight/
最大堆
维护一个大顶堆,每次取出两个石头进行销毁,将销毁后剩余的石头重新加入堆中。
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> o2 - o1);
for (int i = 0; i < stones.length; i++) {
queue.offer(stones[i]);
}
while (queue.size() >= 2) {
int x = queue.poll();
int y = queue.poll();
if (x != y) {
queue.offer(Math.abs(x - y));
}
}
return queue.isEmpty() ? 0 : queue.peek();
}
}