Skip to content

Commit ebf9592

Browse files
authored
solve 더맵게 with java
1 parent 77cb0a2 commit ebf9592

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

더맵게/solution.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public int solution(int[] scoville, int K) {
5+
PriorityQueue<Integer> pq = new PriorityQueue<>();
6+
for (int s : scoville) {
7+
pq.add(s);
8+
}
9+
int answer = 0;
10+
11+
while (pq.size() > 1) {
12+
if (pq.peek() >= K) {
13+
return answer;
14+
}
15+
int first = pq.poll();
16+
int second = pq.poll();
17+
int mixed = first + (second * 2);
18+
pq.add(mixed);
19+
answer++;
20+
}
21+
22+
return pq.peek() >= K ? answer : -1;
23+
}
24+
}

0 commit comments

Comments
 (0)