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()>=2 && pq.peek()<K) {
            int s1 = pq.poll();
            int s2 = pq.poll();
            int sum = s1+s2*2;
            pq.add(sum);
            answer++;
        }
        
        return pq.peek()>=K ? answer : -1;
    }
}

'Problem Solving > Programmers' 카테고리의 다른 글

[Level 2] 행렬 테두리 회전하기  (0) 2022.03.31
[Level 2] 타겟 넘버  (0) 2022.03.31
[Level 2] 기능개발  (0) 2022.03.31
[Level 2] 124 나라의 숫자  (0) 2022.03.31
[Level 2] 멀쩡한 사각형  (0) 2022.03.31