From 5ebbb8a31fee15f0c3bfcc8c35af580248dc4c89 Mon Sep 17 00:00:00 2001 From: SinnoLn Date: Fri, 25 Sep 2026 00:00:13 +0900 Subject: [PATCH] 1046. Last Stone Weight --- .../1046. Last Stone Weight.java" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 "leetcode3/\354\235\264\354\247\204\355\235\254/1046. Last Stone Weight.java" 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