문제
문제 답안
import java.util.*;
class Solution {
public int solution(int[] scoville, int K) {
int answer = 0;
PriorityQueue<Integer> p = new PriorityQueue<>();
for(int i : scoville)
p.add(i);
int n1 = 0;
int n2 = 0;
while(p.peek() < K){
if(p.size() >= 2){
n1 = p.poll();
n2 = p.poll();
p.add(n1 + (n2*2)); // 기존의 큐에서 값을 추가 할 때도 자동으로 작은 우선순위대로 정렬
answer++;
}
else{
if(p.peek() < K)
return -1;
else{
break;
}
}
}
return answer;
}
}
답안 풀이
우선순위 큐 이용.