728x90
풀이
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.stream.Collectors;
class Solution{
public int solution(int[] scoville, int K){
int count = 0;
int firstLowest;
int secondLowest;
int combinedTwoLowestFoods;
PriorityQueue<Integer> priorityQueueLowest = new PriorityQueue<>(Arrays.stream(scoville).boxed().collect(Collectors.toList()));
while(true){
if(priorityQueueLowest.size() == 1 || priorityQueueLowest.size() == 0) return -1;
count++;
firstLowest = priorityQueueLowest.poll();
secondLowest = priorityQueueLowest.poll();
combinedTwoLowestFoods = firstLowest + secondLowest*2;
priorityQueueLowest.add(combinedTwoLowestFoods);
if(K <= priorityQueueLowest.peek()) return count;
}
}
}
출처
728x90
'Algorithm > Programmers' 카테고리의 다른 글
Programmers 스택/큐::레벨2::프린터 (0) | 2022.04.08 |
---|---|
Programmers :: 해시 전화 번호 목록 레벨2 (0) | 2022.03.26 |
Programmers :: 정렬 가장 큰 수 레벨2 (0) | 2022.03.23 |
Programmers :: DFS(깊이 우선 탐색법) 네트워크 레벨 3 (0) | 2022.03.23 |
Programmers :: Greedy(탐욕법) 체육복 레벨1 (0) | 2022.03.15 |