(int[] stones)
| 1 | class Solution { |
| 2 | |
| 3 | public int lastStoneWeight(int[] stones) { |
| 4 | PriorityQueue<Integer> maxHeap = new PriorityQueue(); |
| 5 | for (int stone : stones) maxHeap.add(-stone); |
| 6 | while (maxHeap.size() > 1) { |
| 7 | int stone1 = maxHeap.remove(); |
| 8 | int stone2 = maxHeap.remove(); |
| 9 | if (stone1 != stone2) maxHeap.add(stone1 - stone2); |
| 10 | } |
| 11 | return maxHeap.size() != 0 ? (-maxHeap.remove()) : 0; |
| 12 | } |
| 13 | } |