-
[programmers] 더 맵게(heap)Algorithm/java tip 2021. 3. 6. 14:16
programmers.co.kr/learn/courses/30/lessons/42626
import java.util.PriorityQueue; class Solution { public int solution(int[] scoville, int K) { PriorityQueue<Integer> pq = new PriorityQueue<>(scoville.length); for (int s : scoville) { pq.offer(s); } int cnt = 0; while (!pq.isEmpty() && pq.peek() < K) { int min1 = pq.poll(); if(!pq.isEmpty()) { int min2 = pq.poll() * 2; if(pq.size() == 1 && min2 < K) { return -1; } pq.offer(min1 + min2); cnt++; } } return cnt; } }
'Algorithm > java tip' 카테고리의 다른 글
[programmers] 체육복 (0) 2021.03.06 [programmers] 이중 우선순위 큐 (0) 2021.03.06 [programmers] 소수 찾기 (0) 2021.03.05 107. Binary Tree Level Order Traversal II (0) 2021.03.03 46. Permutations (0) 2021.03.03