[Programmers] 힙(Heap) - 더 맵게

zzenee·2022년 10월 12일
0

Algorithm&Coding-test

목록 보기
28/30
post-thumbnail

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

Problem

Code

import java.util.*;

class Solution {
    public int solution(int[] scoville, int K) {
        int answer = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int scv : scoville) {
            pq.offer(scv);
        }
        while(!pq.isEmpty()) {
            if (pq.size() == 1) {
                if (pq.peek() < K) answer = -1;
                break;
            }
            int first = pq.poll();
            int second = pq.poll();
            if (first >= K) break;
            int scov = first + (second * 2);
            pq.offer(scov);
            answer++;
        }
        
        return answer;
    }
}

Result

profile
꾸준히

0개의 댓글