|
| 1 | +/* |
| 2 | +βοΈ λ¬Έμ μ 보 βοΈ |
| 3 | +λ¬Έμ : 42628 - μ΄μ€μ°μ μμν |
| 4 | +λ 벨 : Level 3 |
| 5 | +λ§ν¬ : https://school.programmers.co.kr/learn/courses/30/lessons/42628 |
| 6 | +*/ |
| 7 | + |
| 8 | +// ANCHOR : 26.02.05 νμ΄ |
| 9 | +class Heap { |
| 10 | + constructor(compare) { |
| 11 | + this.heap = []; |
| 12 | + this.compare = compare; |
| 13 | + } |
| 14 | + |
| 15 | + size() { |
| 16 | + return this.heap.length; |
| 17 | + } |
| 18 | + |
| 19 | + push(value) { |
| 20 | + this.heap.push(value); |
| 21 | + this.siftUp(this.heap.length - 1); |
| 22 | + } |
| 23 | + |
| 24 | + pop() { |
| 25 | + if (this.heap.length === 0) return null; |
| 26 | + const root = this.heap[0]; |
| 27 | + if (this.heap.length === 1) return this.heap.pop(); |
| 28 | + this.heap[0] = this.heap.pop(); |
| 29 | + this.siftDown(0); |
| 30 | + |
| 31 | + return root; |
| 32 | + } |
| 33 | + |
| 34 | + siftUp(i) { |
| 35 | + while (i > 0) { |
| 36 | + const p = Math.floor((i - 1) / 2); |
| 37 | + if (this.compare(this.heap[i], this.heap[p])) { |
| 38 | + this.swap(i, p); |
| 39 | + i = p; |
| 40 | + } else break; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + siftDown(i) { |
| 45 | + if (this.heap.length === 0) return; |
| 46 | + |
| 47 | + while (true) { |
| 48 | + const lc = 2 * i + 1 >= this.heap.length ? null : 2 * i + 1; |
| 49 | + const rc = 2 * i + 2 >= this.heap.length ? null : 2 * i + 2; |
| 50 | + |
| 51 | + let c; |
| 52 | + if (lc === null) break; |
| 53 | + else if (rc === null) c = lc; |
| 54 | + else { |
| 55 | + this.compare(this.heap[lc], this.heap[rc]) ? (c = lc) : (c = rc); |
| 56 | + } |
| 57 | + |
| 58 | + if (this.compare(this.heap[c], this.heap[i])) { |
| 59 | + this.swap(c, i); |
| 60 | + i = c; |
| 61 | + } else break; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + swap(a, b) { |
| 66 | + [this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]]; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function solution(operations) { |
| 71 | + const minHeap = new Heap((a, b) => a.value < b.value); |
| 72 | + const maxHeap = new Heap((a, b) => a.value > b.value); |
| 73 | + |
| 74 | + let id = 0; |
| 75 | + const deleted = new Set(); |
| 76 | + for (const operation of operations) { |
| 77 | + const [op, value] = operation.split(" "); |
| 78 | + if (op === "I") { |
| 79 | + const num = Number(value); |
| 80 | + minHeap.push({ id, value: num }); |
| 81 | + maxHeap.push({ id, value: num }); |
| 82 | + id++; |
| 83 | + } else if (op === "D") { |
| 84 | + if (value === "1") { |
| 85 | + // MAX heapμμ μμ |
| 86 | + while (maxHeap.size() > 0) { |
| 87 | + const x = maxHeap.pop(); |
| 88 | + if (deleted.has(x.id)) continue; |
| 89 | + else { |
| 90 | + deleted.add(x.id); |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + } else if (value === "-1") { |
| 95 | + // MIN heapμμ μμ |
| 96 | + while (minHeap.size() > 0) { |
| 97 | + const x = minHeap.pop(); |
| 98 | + if (deleted.has(x.id)) continue; |
| 99 | + else { |
| 100 | + deleted.add(x.id); |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + function popValid(heap, deleted) { |
| 109 | + while (heap.size() > 0) { |
| 110 | + const node = heap.pop(); |
| 111 | + if (node === null) return null; |
| 112 | + if (deleted.has(node.id)) continue; |
| 113 | + return node; |
| 114 | + } |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + const maxNode = popValid(maxHeap, deleted); |
| 119 | + if (maxNode === null) return [0, 0]; |
| 120 | + const minNode = popValid(minHeap, deleted); |
| 121 | + if (minNode === null) return [0, 0]; |
| 122 | + |
| 123 | + return [maxNode.value, minNode.value]; |
| 124 | +} |
0 commit comments