처음에 배열로 시도를 했으나 효율성 테스트에서 통과하지 못하고

PriorityQueue를 사용해서 통과했다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.*;
 
class Solution {
    public int solution(int[] scoville, int K) {
        int count = 0//섞은 횟수
        PriorityQueue<Integer> minHeap = new PriorityQueue<>(); //최소힙
        
        for(int s : scoville) {
            minHeap.offer(s);
        }
        
        while(minHeap.peek() < K) {
            if(minHeap.size() < 2) { //스코빌 지수가 K보다 작은데 원소가 하나일 경우
                return -1;
            }
            count++;
            minHeap.offer(minHeap.poll() + (minHeap.poll() * 2));
        }
       
        return count;
    }
    
}
cs

 

+ Recent posts