Skip to content

Commit e8ebed6

Browse files
committed
feat : 프로그래머스 힙
1 parent 511633d commit e8ebed6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package sgyj.programmers.yeji.heap;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class Solution42626 {
6+
public static int solution(int[] scoville, int K) {
7+
int answer = 0;
8+
PriorityQueue<Integer> pQ = new PriorityQueue<>();
9+
for(int sco : scoville){
10+
pQ.add(sco);
11+
}
12+
13+
int compare = pQ.poll();
14+
while(!pQ.isEmpty()){
15+
if(compare >= K) return answer;
16+
int next = pQ.poll() * 2;
17+
pQ.add(compare+next);
18+
compare = pQ.poll();
19+
answer++;
20+
}
21+
22+
return compare>=K ? answer : -1;
23+
}
24+
public static void main ( String[] args ) {
25+
int[] scoville = {1, 2, 3, 9, 10, 12};
26+
int K = 7;
27+
System.out.println(solution(scoville,K));
28+
}
29+
}

0 commit comments

Comments
 (0)