본문 바로가기
알고리즘/프로그래머스

더 맵게 (자바)

by 김어찐 2021. 9. 8.
728x90
package prog;

import java.util.PriorityQueue;
import java.util.Queue;

public class 더맵게 {
    public static void main(String[] args) {
        Solution_더맵게 s = new Solution_더맵게();
        System.out.println(s.solution(new int[] {1, 2, 3, 9, 10, 12},7));
    }
}

class Solution_더맵게 {
    public int solution(int[] scoville, int K) {
        int answer = 0;
        Queue<Integer> pq = new PriorityQueue<>();
        for (int i : scoville) {
            pq.add(i);
        }
        while(true){
            if(pq.size()<2 || pq.peek()>=K){
                break;
            }
            else{
                int a = pq.poll();
                int b = pq.poll();
                int result = a+ b*2;
                pq.add(result);
                answer++;
            }
        }
        if(pq.peek()<K){
            return -1;
        }
        return answer;
    }
}
728x90

'알고리즘 > 프로그래머스' 카테고리의 다른 글

표편집 (자바)  (0) 2021.09.12
짝지어 제거하기 (자바)  (0) 2021.09.09
타겟넘버 (자바)  (0) 2021.09.08
기능개발 (자바)  (0) 2021.09.07
124 나라의 숫자 (자바)  (0) 2021.09.06