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
'Problem Solving > Programmers' 카테고리의 다른 글
[프로그래머스, 120891] 369게임 (java) (0) | 2025.02.11 |
---|---|
[프로그래머스, 120843] 공 던지기 (java) (0) | 2025.02.10 |
[프로그래머스, 181883] 수열과 구간 쿼리 1 (java) (0) | 2025.02.10 |
[프로그래머스, 181885] 할 일 목록 (java) (0) | 2025.02.10 |
[프로그래머스, 181886] 5명씩 (java) (0) | 2025.02.10 |