09-11. 힙(Heap) 자료구조

우선순위 큐를 기반으로 푸는 문제

0. 알아야 하는 코드

1) Heap 구현

import java.util.PriorityQueue;

// 기본 최소 힙 (오름차순)
PriorityQueue<Integer> minHeap = new PriorityQueue<>();

// 최대 힙 (내림차순)
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());

// 복잡한 객체로 힙 만들기 (오름차순)
PriorityQueue<Student> studentHeap = new PriorityQueue<>(
    (s1, s2) -> Integer.compare(s1.getScore(), s2.getScore())
);

2) Heap 기본 연산

PriorityQueue<Integer> heap = new PriorityQueue<>();

// 원소 추가
heap.offer(10);
heap.add(20);

// 최상위 원소 확인 (제거하지 않음)
int top = heap.peek();

// 최상위 원소 뽑고 제거
int removedTop = heap.poll();

heap.remove(Element);   // 해당 Element 탐색해서 첫번째 제거 
heap.removeEq(Element); // 해당 Element 탐색해서 전부 제거
heap.clear();           // 초기화

// 힙 크기 확인
int size = heap.size();

// 힙이 비어있는지 확인
boolean isEmpty = heap.isEmpty();

1. 더 맵게

  • 프로그래머스 Lv.2 / 체감난이도 Lv.1

  • 효율성을 위한 우선순위 큐 사용 문제

2. 야근지수

  • 프로그래머스 Lv.3 / 체감난이도 Lv.2

  • 기본적인 문제로 하나씩 뽑아서 게산하는 데 우선순위가 있음

3. 이중우선순위 큐

  • 프로그래머스 Lv.3 / 체감난이도 Lv.3

  • 문제 자체가 이중우선순위 큐 문제

4. 디스크 컨트롤러 문제

  • 프로그래머스 Lv.3 / 체감난이도 Lv.3

  • 문제에 "우선순위 디스크 컨트롤러라는 가상의 장치를 이용한다고 가정"

Last updated