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

HaYeong Jang·2021년 2월 27일
0
post-thumbnail

🔗 문제링크

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

👩🏻‍💻 코드

import java.io.*;
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.add(s);
        }

        while (pq.size() >= 2) {
            int min = pq.poll();
            int min2 = pq.poll();

            if (min >= K) break;
            pq.add(min + min2 * 2);
            answer++;
        }

        if (pq.size() == 1 && pq.poll() < K) {
            answer = -1;
        }

        return answer;
    }
}

📝 정리

PriorityQueue를 사용하지 않으면 효율성이 안 좋게 나오는 문제였다.

profile
기억하기 위해 기록하는 개발로그👣

0개의 댓글