[java] 프로그래머스 - 더 맵게

세상을 바꾸는 개발자·2023년 3월 26일
0

[문제링크 - 프로그래머스 - 더 맵게] https://born2bedeveloper.tistory.com/11

import java.util.PriorityQueue;

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

        for(int s : scoville){
            pq.offer(s);
        }

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

            int a = pq.poll();
            int b = pq.poll();

            pq.offer(a + b * 2);
            answer++;
        }
        return answer;
    }
}
profile
초심 잃지 않기

0개의 댓글