Files
leetcode/1046.last-stone-weight.java
2025-10-23 16:32:13 +08:00

25 lines
518 B
Java

/*
* @lc app=leetcode id=1046 lang=java
*
* [1046] Last Stone Weight
*/
// @lc code=start
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b)->b-a);
for(int i = 0; i < stones.length; i++) {
pq.add(stones[i]);
}
int a, b;
while(pq.size() > 1) {
a = pq.poll();
b = pq.poll();
pq.add(a < b?b-a:a-b);
}
return pq.peek();
}
}
// @lc code=end