Skip to content

Commit 6405f43

Browse files
Merge pull request #2084 from CodingTestStudy2/이진희
[이진희] Day25
2 parents 82ca92e + 5ebbb8a commit 6405f43

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)