#문제 설명
#문제 풀이
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
int sum = 0;
for(int i = 0; i < stones.length; i++){
maxHeap.add(stones[i]);
sum += stones[i];
}
int size = maxHeap.size();
while(maxHeap.size() > 1){
int num1 = 0;
int num2 = 0;
if(maxHeap.size() >= 2){
num1 = maxHeap.poll();
num2 = maxHeap.poll();
}
maxHeap.add(num1 - num2);
sum -= num1 + num2 - (num1 - num2);
}
return sum;
}
}
'LeetCode > Heap' 카테고리의 다른 글
[Medium] 703. Kth Largest Element in a Stream (아마존 코딩 테스트 기출 문제) (0) | 2024.04.03 |
---|---|
[Hard] 295. Find Median from Data Stream (Microsoft 기출 문제) (0) | 2022.10.13 |
[Medium] 451. Sort Characters By Frequency (0) | 2022.10.11 |
[Medium] 347. Top K Frequent Elements (애플/MS/메타/아마존) (0) | 2022.10.02 |
[Medium] Kth Largest Element in an Array (아마존/구글) (0) | 2022.10.02 |