본문 바로가기
algorithm/acmicpc

[백준 알고리즘] 1927번 최소 힙

by 무대포 개발자 2018. 6. 25.
728x90
반응형

1. Point

  • Heap 자료구조 시간복잡도
    • 삽입 : 맨 마지막 Index 에 넣고, 부모 노드랑 비교. 부모 노드 비교 식은 (index -1) / 2 로 1/2 씩 비교하게 되니 logN 이 됨.
    • 삭제 : 맨 마지막 Index 를 Root 로 옮겨서, 자식 노드 2개씩 비교해가면서 내려가니 logN 이 됨.

2. Source

public static void main(String[] args)
{
 PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
 Scanner sc = new Scanner(System.in);
 int N = sc.nextInt();
 for (int idx = 0 ; idx < N ; idx++)
 {
  int x = sc.nextInt();
  if (x == 0)
  {
   if (queue.isEmpty()) 
    System.out.println(0);
   else 
    System.out.println(queue.poll());
  }
  else
  {
   queue.offer(x);
  }
 }
}

'algorithm > acmicpc' 카테고리의 다른 글

백준 1002 터렛  (0) 2020.07.10
[백준] 1780번 종이의 개수  (0) 2018.06.26
[BOJ] 9465 스티커 (java)  (0) 2018.01.30
[백준 알고리즘] 1005번 ACM Craft  (0) 2018.01.29
[백준 알고리즘] 1004번 어린왕자 (java)  (0) 2018.01.29

댓글