inblog logo
|
stupefyee
    알고리즘문제풀기

    [JAVA 문제 풀이] 341. 더 맵게

    프로그래머스 (42626)
    Stupefyee's avatar
    Stupefyee
    Jun 25, 2025
    [JAVA 문제 풀이] 341. 더 맵게
    Contents
    내가 작성한 코드
    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/42626
    notion image
     

    내가 작성한 코드

    import java.util.*; class Solution { public int solution(int[] scoville, int K) { PriorityQueue<Integer> pq = new PriorityQueue<>(); for (int s : scoville) { pq.add(s); } int answer = 0; // 가장 맵지 않은 음식이 k이하이고 큐에 음식이 2개 이상 있으면 반복 while (pq.size() >= 2 && pq.peek() < K) { int first = pq.poll(); // 가장 맵지 않은 음식 int second = pq.poll(); // 두 번째로 맵지 않은 음식 int mix = first + (second * 2); // 섞기 pq.add(mix); // 섞은 음식 다시 넣기 answer++; } // 남은 가장 낮은 음식도 K 이상인지 확인 return pq.peek() >= K ? answer : -1; } }
    Share article
    Contents
    내가 작성한 코드

    stupefyee

    RSS·Powered by Inblog