[프로그래머스, 42626] 더 맵게 (java)
Problem Solving/Programmers

[프로그래머스, 42626] 더 맵게 (java)

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/42626

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

728x90

메모리: 128 MB, 시간: 1656.40 ms

사용 알고리즘: 자료구조

import java.util.*;

class Solution {
    public int solution(int[] scoville, int K) {
        
        // 스코빌 지수를 오름차순으로 정렬해 담을 우선순위 큐
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for(int s : scoville) pq.add(s);
        
        int answer = 0;
        
        // 가장 맵지 않은 음식의 스코빌 지수가 K 이상이 될 때까지
        int s1, s2;
        while(pq.peek() < K && pq.size() >= 2) {
            // 가장 맵지 않은 음식
            s1 = pq.poll();
            // 두 번째로 맵지 않은 음식
            s2 = pq.poll();
            
            // 섞기
            pq.add(s1 + s2 * 2);
            answer++;
        }
        
        return pq.peek() < K ? -1 : answer;
    }
}
728x90