알고리즘 공부[Javascript]/백준

[백준] 11279번 / 실버2 / 최대 힙 / Node.js

Kevinkb 2021. 10. 9. 23:29

문제

널리 잘 알려진 자료구조 중 최대 힙이 있다. 최대 힙을 이용하여 다음과 같은 연산을 지원하는 프로그램을 작성하시오.

  1. 배열에 자연수 x를 넣는다.
  2. 배열에서 가장 큰 값을 출력하고, 그 값을 배열에서 제거한다.

프로그램은 처음에 비어있는 배열에서 시작하게 된다.

입력

첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0이라면 배열에서 가장 큰 값을 출력하고 그 값을 배열에서 제거하는 경우이다. 입력되는 자연수는 231보다 작다.

출력

입력에서 0이 주어진 회수만큼 답을 출력한다. 만약 배열이 비어 있는 경우인데 가장 큰 값을 출력하라고 한 경우에는 0을 출력하면 된다.

예제 입력

13
0
1
2
0
0
3
2
1
0
0
0
0
0

예제 출력

0
2
1
3
2
1
0
0

소스 코드

const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n').map(Number);
const N = input.shift();
const answer = [];
const heap = [0];
const root = 1;
let size = 0;

const swap = (idx1, idx2) => {
  let tmp = heap[idx1];
  heap[idx1] = heap[idx2];
  heap[idx2] = tmp;
}

const parent = (idx) => Math.floor(idx / 2);
const leftChild = (idx) => idx * 2;
const rightChild = (idx) => idx * 2 + 1;

const insert = (el) => {
  heap[++size] = el;
  let index = size;

  while (index > 1) {
    let parentIdx = parent(index);

    if (heap[parentIdx] < heap[index]) {
      swap(parentIdx, index);
      index = parentIdx;
    } else {
      break;
    }
  }
}

const getMax = () => {
  if (size === 0) return 0;
  let result = heap[1];
  let index = 1;
  heap[index] = heap.pop();
  size--;

  while (index <= size) {
    if (leftChild(index) <= size) {
      let childIdx = leftChild(index);

      if (rightChild(index) <= size) {
        childIdx = heap[leftChild(index)] > heap[rightChild(index)] ? leftChild(index) : rightChild(index);
      }

      if (heap[childIdx] > heap[index]) {
        swap(childIdx, index);
        index = childIdx;
      } else {
        break;
      }
    } else {
      break;
    }
  }

  return result
}

for (let i = 0; i < input.length; i++) {
  if (input[i] === 0) {
    answer.push(getMax());
  } else {
    insert(input[i]);
  }
}
console.log(answer.join('\n'));