Last Stone Weight LeetCode solution in Java
1class Solution {2public int lastStoneWeight(int[] stones) {3PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> Integer.compare(y, x));4for (int i = 0; i < stones.length; i++) {5pq.add(stones[i]);6}7while (pq.size() > 1) {8int r1 = pq.poll();9int r2 = pq.poll();10if (r1 != r2) pq.add(r1 - r2);11}12return (pq.isEmpty()) ? 0 : pq.poll();13}14}