25 lines
518 B
Java
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
|
|
|