最后一块石头的重量
leetcode1046. 最后一块石头的重量
题目描述
解题思路
代码演示
/**
* 求最后一块石头的大小
* @param stones
* @return
*/
public int lastStoneWeight(int[] stones) {
if (stones.length == 1){
return stones[0];
}
//优先级队列,从大到小排列
PriorityQueue<Integer> queue = new PriorityQueue<>((a,b)->b - a);
for (int stone : stones){
queue.offer(stone);
}
while (queue.size() > 1){
Integer a = queue.poll();
Integer b = queue.poll();
if (a == b ){
continue;
}else{
queue.offer(a - b);
}
}
return queue.size() == 0 ? 0 : queue.poll();
}
往期经典算法
leetcode123. 买卖股票的最佳时机 III
leetcode188. 买卖股票的最佳时机 IV
leetcode312. 戳气球
leetcode82. 删除排序链表中的重复元素 II
leetcode787. K 站中转内最便宜的航班
leetcode863. 二叉树中所有距离为 K 的结点
leetcode63. 不同路径 II
leetcode62. 不同路径