2 parents 82ca92e + 5ebbb8a commit 6405f43Copy full SHA for 6405f43
1 file changed
leetcode3/이진희/1046. Last Stone Weight.java
@@ -0,0 +1,24 @@
1
+import java.util.PriorityQueue;
2
+import java.util.Collections;
3
+
4
+class Solution {
5
+ public int lastStoneWeight(int[] stones) {
6
7
+ PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
8
9
+ for (int stone : stones) {
10
+ maxHeap.add(stone);
11
+ }
12
13
+ while (maxHeap.size() > 1) {
14
+ int first = maxHeap.poll();
15
+ int second = maxHeap.poll();
16
17
+ if (first != second) {
18
+ maxHeap.add(first - second);
19
20
21
22
+ return maxHeap.isEmpty() ? 0 : maxHeap.poll();
23
24
+}
0 commit comments