문제 풀이 : 2021.05.21
배열과 sort를 이용해서 풀면 정답은 맞출수 있지만 효율성 테스트를 통과할 수 없음
-> 우선순위 큐 (priority queue) 를 이용함
import java.util.*;
class Solution {
PriorityQueue<Integer> pq = new PriorityQueue<>();
public int solution(int[] scoville, int K) {
int answer = 0;
for(int i = 0;i<scoville.length;i++)
pq.offer(scoville[i]);
while(true){
if(pq.peek() >= K) break;
if(pq.size() < 2) return -1;
int a = pq.poll();
int b = pq.poll();
pq.offer(a+2*b);
answer++;
}
return answer;
}
}
문제 출처 링크