import java.util.Collections;
import java.util.PriorityQueue;
public class Algorithm {
public static void main(String[] args) {
int[] arr = {2,7,4,1,8,1};
System.out.println(new Solution().lastStoneWeight(arr));
}
}
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < stones.length; i++) {
pq.add(stones[i]);
}
while (pq.size() > 1){
int y = pq.poll();
int x = pq.poll();
if (y > x) {
pq.add(y - x);
}
}
if (pq.size() == 1){
return pq.poll();
}
else {
return 0;
}
}
}
https://leetcode-cn.com/problems/last-stone-weight/