We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 98c02ec commit c069298Copy full SHA for c069298
minjeong/Heap/2025-05-31-[PGS]-더맵게(java).java
@@ -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
29
30
31
32
+ return -1;
33
34
+}
0 commit comments