시도 1. Java 에서 구현된 Heap → PriorityQueue 사용
import java.util.PriorityQueue;
class Solution {
public int solution(int[] scoville, int K) {
int answer = 0;
PriorityQueue<Integer> heap = new PriorityQueue<>();
for (int item : scoville) {
heap.add(item);
}
while (heap.peek() < K) {
answer++;
Integer first = heap.poll();
Integer second = heap.poll();
if (first == null || second == null) {
return -1;
}
int score = first + second * 2;
heap.add(score);
}
return answer;
}
}