Skip to content

Commit c069298

Browse files
committed
[PGS] 더 맵게 (Java) / Level 2 / 30분 / 성공
1 parent 98c02ec commit c069298

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.Arrays;
2+
import java.util.PriorityQueue;
3+
4+
class Solution {
5+
public int solution(int[] scoville, int K) {
6+
int answer = 0;
7+
Arrays.sort(scoville);
8+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
9+
10+
// scoville 배열을 minHeap으로 옮기기
11+
for (int s: scoville) {
12+
minHeap.offer(s);
13+
}
14+
15+
// minHeap의 크기가 1보다 작아질 때까지 반복
16+
while (minHeap.size() > 1) {
17+
// 이미 K보다 크다면 바로 return
18+
if (minHeap.peek() >= K) {
19+
return answer;
20+
}
21+
int a = minHeap.poll();
22+
int b = minHeap.poll();
23+
minHeap.offer(a + b * 2);
24+
answer++;
25+
}
26+
27+
// 마지막 하나도 확인해야 한다.
28+
if (minHeap.peek() >= K) {
29+
return answer;
30+
}
31+
32+
return -1;
33+
}
34+
}

0 commit comments

Comments
 (0)