알고리즘 문제 풀이를 블로그에 올리는 이유는 풀이, 코드를 기록하기 위함이니
앞으로 문제를 다 긁어오기보다 링크만 두고 풀이가 잘 보이도록 포스팅 할 예정입니다!
이 문제는 주어진 음식 스코빌 지수 값들 중
가장 작은 값 두 개를 결합하여 새로운 음식을 만들고
이를 반복하여 모든 음식이 최소 스코빌 지수를 넘도록할 때
필요한 최소 반복 횟수를 구하는 문제이다.
import java.util.PriorityQueue;
public class Solution {
public static void main(String[] args) {
int[] scoville = {1, 2, 3, 9, 10, 12};
int K = 7;
System.out.println(solution(scoville, K));
}
public static int solution(int[] scoville, int K) {
PriorityQueue<Integer> foods = new PriorityQueue<>();
for(int s : scoville) {
foods.add(s);
}
int cnt = 0;
while(foods.peek() < K) {
int f1 = foods.poll();
int f2 = foods.poll();
int newFood = f1 + (f2 * 2);
if(foods.isEmpty() && newFood < K) {
return -1;
}
foods.add(newFood);
cnt++;
}
return cnt;
}
}