알고리즘
(프로그래머스)(자바) 더 맵게
아이셩짱셩
2021. 7. 7. 16:25
728x90
코딩테스트 연습-힙(Heap)-더 맵게
https://programmers.co.kr/learn/courses/30/lessons/42626
import java.util.*;
class Solution {
public int solution(int[] scoville, int K) {
int answer = 0;
Queue<Integer> pq = new PriorityQueue(scoville.length);
for(int s : scoville){
pq.offer(s);
}
int times = 0;
while(pq.peek() <K && pq.size() > 1){
int s1 = pq.poll();
int s2 = pq.poll();
pq.offer(s1+s2*2);
times++;
}
if(pq.size() <=1 && pq.peek()<K){
return -1;
}
answer = times;
return answer;
}
}
728x90