[Algorithm - Programmers] 더 맵게

nunu·2023년 12월 17일
0

Algorithm

목록 보기
122/142

https://school.programmers.co.kr/learn/courses/30/lessons/42626

제출 코드

import java.util.PriorityQueue;
class Solution {
    public int solution(int[] scoville, int K) {
        int answer = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int i = 0; i < scoville.length; i++) {
            pq.offer(scoville[i]);
        }

        while (pq.peek() < K) {
            if (pq.size() < 2) {
               return -1;
            }

            int temp1 = pq.poll();
            int temp2 = pq.poll();

            int nScovile = temp1 + (temp2 * 2);
            pq.offer(nScovile);
            answer++;
        }
        return answer;
    }
}
profile
Hello, I'm nunu

0개의 댓글