leetcode 1046. Last Stone Weight
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>((a, b)-> b - a);
for (int num: stones){
maxHeap.offer(num);
}
while (!maxHeap.isEmpty()){
if (maxHeap.size()>=2){
maxHeap.offer(maxHeap.poll()-maxHeap.poll()); //top two are the two largest,we will just smash these two
}else{
return maxHeap.poll();//when there is only one element
}
}
return -1;
}
}