🥞 BE
home

42626_더 맵게

담당자
완료 여부
Solved
요약
날짜
2024/07/12
태그
자료구조
난이도
Lv2
출처
프로그래머스

코드

import java.util.PriorityQueue; class Solution { public int solution(int[] scoville, int K) { // 우선순위 큐를 사용하여 최소 힙을 생성 PriorityQueue<Integer> pq = new PriorityQueue<>(); // 모든 스코빌 지수를 우선순위 큐에 추가 for (int s : scoville) { pq.offer(s); } int count = 0; // 최소 힙의 최솟값이 K 이상이 될 때까지 반복 while (pq.size() > 1 && pq.peek() < K) { int first = pq.poll(); int second = pq.poll(); int newScoville = first + (second * 2); pq.offer(newScoville); count++; } // 모든 음식의 스코빌 지수가 K 이상이 되지 못할 경우 if (pq.peek() < K) { return -1; } return count; } }
Java
복사

문제 해결 아이디어