1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public int lastStoneWeight(int[] stones) { Queue<Integer> queue = new PriorityQueue<>((o1,o2)->{ return o2.compareTo(o1); });
for(int w :stones){ queue.offer(w); } while (queue.size() > 1){ int w1 = queue.poll(); int w2 = queue.poll(); if (w1 == w2){ continue; } int r = Math.abs(w1 - w2); queue.offer(r); }
return queue.isEmpty() ? 0 : queue.poll(); } }
|