더 맵게


프로그래머스 [더 맵게]

문제보기
Alt text

소스코드

import java.util.PriorityQueue;

class Solution {
    public int solution(int[] scoville, int K) {
        int answer = 0;
        PriorityQueue<Integer> queue = new PriorityQueue<>();

        for (int value : scoville) {
            queue.add(value);
        }

        while (!queue.isEmpty()) {
            int fstValue = queue.poll();

            if (fstValue >= K) {
                return answer;
            }
            if (queue.isEmpty()) return -1;

            int sndValue = queue.poll();
            int calc = fstValue + (sndValue * 2);
            answer++;

            queue.add(calc);
        }

        return -1;
    }
}