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
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
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] - 더 맵게 (힙)
더 맵게 (힙)
from heapq import heapify, heappush, heappop def solution(scoville, K): cnt=0 heapify(scoville) while True: now=heappop(scoville) if now>=K: break if len(scoville)
karla.tistory.com
728x90