[프로그래머스] 힙 - 더 맵게(자바)

Rena·2022년 4월 7일
0

알고리즘 문제풀이

목록 보기
26/45
import java.util.PriorityQueue;

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

        for(int ascoville : scoville) {
            heap.offer(ascoville);
        }

        while (heap.peek() <= K) {
            if(heap.size()==1) return -1;
            int a = heap.poll();
            int b = heap.poll();

            int result = a + (b*2);
            heap.offer(result);
            answer++;
        }
        return answer;
    }

    public static void main(String[] args) {
        int[] scoville = {1,2,3,9,10,12};
        int K = 7;
        Solution st = new Solution();
        System.out.println(st.solution(scoville,K));
    }
}
profile
일을 사랑하고 싶은 개발자

0개의 댓글