- 난이도: Lv2
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/42626
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/2/더 맵게
풀이 시간 : 분
import java.util.*;
class Solution {
public int solution(int[] scoville, int K) {
int answer = 0;
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int s : scoville) {
pq.add(s);
}
while (pq.size() > 1 && pq.peek() < K) {
int first = pq.poll();
int second = pq.poll();
int newScoville = first + (second * 2);
pq.add(newScoville);
answer++;
}
if (pq.peek() < K) {
return -1;
}
return answer;
}
}