슈콩

[프로그래머스] Lv.2 더 맵게 본문

Algorithms/Programmers

[프로그래머스] Lv.2 더 맵게

shukong 2025. 10. 16. 17:47

 

 

[문제]

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

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

[소스 코드]

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.offer(s);
        while(pq.size()>1 && pq.peek()<K){
            int a = pq.poll();
            int b = pq.poll() * 2;
            pq.offer(a+b);
            answer++;
        }
        if(pq.peek()<K) return -1;
        return answer;
    }
}