diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/1046. Last Stone Weight.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/1046. Last Stone Weight.java" new file mode 100644 index 00000000..3614f40a --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/1046. Last Stone Weight.java" @@ -0,0 +1,24 @@ +import java.util.PriorityQueue; +import java.util.Collections; + +class Solution { + public int lastStoneWeight(int[] stones) { + + PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); + + for (int stone : stones) { + maxHeap.add(stone); + } + + while (maxHeap.size() > 1) { + int first = maxHeap.poll(); + int second = maxHeap.poll(); + + if (first != second) { + maxHeap.add(first - second); + } + } + + return maxHeap.isEmpty() ? 0 : maxHeap.poll(); + } +} \ No newline at end of file