728x90
//오름차순(우선순위가 낮은 숫자 순)
PriorityQueue<Integer> pq = new PriorityQueue<>();
//내림차순(우선순위가 높은 숫자 순)
PriorityQueue<Integer> pqHightest = new PriorityQueue<>(Collections.reverseOrder());
pq.add(1);
pq.offer(2);
pq.add(3);
pq.poll(); // 첫번째 값을 반환하고 제거 비어있다면 null
System.out.println(pq); // [2, 3]
pqHightest.add(1);
pqHightest.add(2);
pqHightest.add(3);
pqHightest.peek(); // 첫번째 값 참조 3
pqHightest.remove(); // 첫번째 값 제거
System.out.println(pqHightest); // [2, 1]
pqHightest.clear(); // 초기화
https://school.programmers.co.kr/learn/courses/30/lessons/42626?language=java
import java.util.*;
class Solution {
public int solution(int[] scoville, int K) {
PriorityQueue<Integer> q = new PriorityQueue<>();
for(int i = 0; i < scoville.length; i++)
q.add(scoville[i]);
int cnt = 0;
while(q.size() > 1 && q.peek() < K){
int weakHot = q.poll();
int secondWeakHot = q.poll();
q.add(weakHot + (secondWeakHot * 2));
cnt++;
}
if(q.size() <= 1 && q.peek() < K)
cnt = -1;
return cnt;
}
}
2023.07.05 - [Coding Test/programmers] - 더 맵게 (힙)
728x90