|
| 1 | +const fs = require("fs"); |
| 2 | +const filePath = |
| 3 | + process.platform === "linux" ? "/dev/stdin" : `${__dirname}/input.txt`; |
| 4 | +let input = fs.readFileSync(filePath).toString().trim().split("\n"); |
| 5 | + |
| 6 | +const n = Number(input.shift()); |
| 7 | +const arr = input.map((x) => Number(x.trim())); |
| 8 | +const answer = []; |
| 9 | + |
| 10 | +class MinHeap { |
| 11 | + constructor() { |
| 12 | + this.items = []; |
| 13 | + } |
| 14 | + |
| 15 | + swap(index1, index2) { |
| 16 | + let temp = this.items[index1]; // items의 index1의 값을 temp(임시공간)에 담음 |
| 17 | + this.items[index1] = this.items[index2]; // index1에 index2의 값을 저장 |
| 18 | + this.items[index2] = temp; // index2에 아까 index1의 값을 temp에 넣어놓은 값을 저장 |
| 19 | + } |
| 20 | + |
| 21 | + //부모 인덱스 구하는 메소드 |
| 22 | + parentIndex(index) { |
| 23 | + return Math.floor((index - 1) / 2); |
| 24 | + } |
| 25 | + |
| 26 | + //왼쪽 자식 인덱스 구하는 메소드 |
| 27 | + leftChildIndex(index) { |
| 28 | + return index * 2 + 1; |
| 29 | + } |
| 30 | + |
| 31 | + //오른쪽 자식 인덱스 구하는 메소드 |
| 32 | + rightChildIndex(index) { |
| 33 | + return index * 2 + 2; |
| 34 | + } |
| 35 | + |
| 36 | + //부모 노드 구하는 메소드 |
| 37 | + parent(index) { |
| 38 | + return this.items[this.parentIndex(index)]; |
| 39 | + } |
| 40 | + |
| 41 | + //왼쪽 자식 노드 구하는 메소드 |
| 42 | + leftChild(index) { |
| 43 | + return this.items[this.leftChildIndex(index)]; |
| 44 | + } |
| 45 | + |
| 46 | + //오른쪽 자식 노드 구하는 메소드 |
| 47 | + rightChild(index) { |
| 48 | + return this.items[this.rightChildIndex(index)]; |
| 49 | + } |
| 50 | + |
| 51 | + //최대 힙의 경우 최댓값을 반환하고 최소 힙의 경우 최솟값을 반환하는 메소드 |
| 52 | + peek() { |
| 53 | + return this.items[0]; |
| 54 | + } |
| 55 | + |
| 56 | + //힙의 크기(항목 개수)를 반환하는 메소드 |
| 57 | + size() { |
| 58 | + return this.items.length; |
| 59 | + } |
| 60 | + |
| 61 | + // MinHeap 클래스는 Heap 클래스를 상속받았으므로 Heap 클래스의 메소드를 모두 사용할 수 있다. |
| 62 | + bubbleUp() { |
| 63 | + let index = this.items.length - 1; |
| 64 | + |
| 65 | + while ( |
| 66 | + this.parent(index) !== undefined && |
| 67 | + this.parent(index) > this.items[index] |
| 68 | + ) { |
| 69 | + this.swap(index, this.parentIndex(index)); |
| 70 | + index = this.parentIndex(index); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + bubbleDown() { |
| 75 | + let index = 0; |
| 76 | + |
| 77 | + while ( |
| 78 | + this.leftChild(index) !== undefined && |
| 79 | + (this.leftChild(index) < this.items[index] || |
| 80 | + this.rightChild(index) < this.items[index]) |
| 81 | + ) { |
| 82 | + let smallerIndex = this.leftChildIndex(index); |
| 83 | + |
| 84 | + if ( |
| 85 | + this.rightChild(index) !== undefined && |
| 86 | + this.rightChild(index) < this.items[smallerIndex] |
| 87 | + ) { |
| 88 | + smallerIndex = this.rightChildIndex(index); |
| 89 | + } |
| 90 | + |
| 91 | + this.swap(index, smallerIndex); |
| 92 | + |
| 93 | + index = smallerIndex; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + add(item) { |
| 98 | + this.items[this.items.length] = item; |
| 99 | + this.bubbleUp(); |
| 100 | + } |
| 101 | + |
| 102 | + poll() { |
| 103 | + let item = this.items[0]; // 첫번째 원소 keep |
| 104 | + this.items[0] = this.items[this.items.length - 1]; // 맨 마지막 원소를 첫번째 원소로 복사 |
| 105 | + this.items.pop(); // 맨 마지막 원소 삭제 |
| 106 | + this.bubbleDown(); |
| 107 | + |
| 108 | + return item; // keep해둔 값 반환 |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +const minHeap = new MinHeap(); |
| 113 | +minHeap.add(1); |
| 114 | +minHeap.add(10); |
| 115 | +minHeap.add(5); |
| 116 | +minHeap.add(100); |
| 117 | +minHeap.add(8); |
| 118 | +console.log(minHeap); //array(5) [1, 8, 5, 100, 10] |
| 119 | +console.log(minHeap.poll()); // 1 |
| 120 | +console.log(minHeap.poll()); // 5 |
| 121 | +console.log(minHeap.poll()); // 8 |
| 122 | +console.log(minHeap.poll()); // 10 |
| 123 | +console.log(minHeap.poll()); // 100 |
| 124 | +console.log(minHeap.poll()); // 100 |
| 125 | +console.log(minHeap); // array(0) |
| 126 | +// for (let i = 0; i < n; i++) { |
| 127 | +// if (arr[i] !== 0) { |
| 128 | +// minHeap.add(arr[i]); |
| 129 | +// } else { |
| 130 | +// if (minHeap.size() === 0) { |
| 131 | +// answer.push(0); |
| 132 | +// } else { |
| 133 | +// answer.push(minHeap.poll()); |
| 134 | +// } |
| 135 | +// } |
| 136 | +// } |
| 137 | + |
| 138 | +// console.log(answer.join("\n")); |
0 commit comments