|
| 1 | +// last test case failed |
| 2 | +// Time complexity: O(logn) |
| 3 | +// Space complexity: O(n) |
| 4 | + |
| 5 | +class MinHeap { |
| 6 | + constructor() { |
| 7 | + this.heap = [null]; |
| 8 | + } |
| 9 | + |
| 10 | + get root() { |
| 11 | + if (this.length === 1) { |
| 12 | + return null; |
| 13 | + } |
| 14 | + |
| 15 | + return this.heap[1]; |
| 16 | + } |
| 17 | + |
| 18 | + get length() { |
| 19 | + return this.heap.length; |
| 20 | + } |
| 21 | + |
| 22 | + push(value) { |
| 23 | + this.heap.push(value); |
| 24 | + |
| 25 | + let current = this.heap.length - 1; |
| 26 | + let parent = Math.floor(current / 2); |
| 27 | + |
| 28 | + while (parent && this.heap[current] < this.heap[parent]) { |
| 29 | + [this.heap[current], this.heap[parent]] = [ |
| 30 | + this.heap[parent], |
| 31 | + this.heap[current], |
| 32 | + ]; |
| 33 | + current = parent; |
| 34 | + parent = Math.floor(current / 2); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pop() { |
| 39 | + if (this.heap.length === 1) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + if (this.heap.length === 2) { |
| 44 | + return this.heap.pop(); |
| 45 | + } |
| 46 | + |
| 47 | + const rv = this.heap[1]; |
| 48 | + this.heap[1] = this.heap.pop(); |
| 49 | + |
| 50 | + let current = 1; |
| 51 | + let left = current * 2; |
| 52 | + let right = left + 1; |
| 53 | + |
| 54 | + while ( |
| 55 | + (this.heap[left] && this.heap[current] > this.heap[left]) || |
| 56 | + (this.heap[right] && this.heap[current] > this.heap[right]) |
| 57 | + ) { |
| 58 | + if (this.heap[right] && this.heap[right] < this.heap[left]) { |
| 59 | + [this.heap[right], this.heap[current]] = [ |
| 60 | + this.heap[current], |
| 61 | + this.heap[right], |
| 62 | + ]; |
| 63 | + current = right; |
| 64 | + } else { |
| 65 | + [this.heap[left], this.heap[current]] = [ |
| 66 | + this.heap[current], |
| 67 | + this.heap[left], |
| 68 | + ]; |
| 69 | + current = left; |
| 70 | + } |
| 71 | + |
| 72 | + left = current * 2; |
| 73 | + right = left + 1; |
| 74 | + } |
| 75 | + |
| 76 | + return rv; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +var MedianFinder = function () { |
| 81 | + this.leftMinHeap = new MinHeap(); |
| 82 | + this.rightMinHeap = new MinHeap(); |
| 83 | +}; |
| 84 | + |
| 85 | +/** |
| 86 | + * @param {number} num |
| 87 | + * @return {void} |
| 88 | + */ |
| 89 | +MedianFinder.prototype.addNum = function (num) { |
| 90 | + const rightMinValue = this.rightMinHeap.root; |
| 91 | + |
| 92 | + if (num >= rightMinValue) { |
| 93 | + this.rightMinHeap.push(num); |
| 94 | + } else { |
| 95 | + this.leftMinHeap.push(num * -1); |
| 96 | + } |
| 97 | + |
| 98 | + if (this.rightMinHeap.length - this.leftMinHeap.length > 1) { |
| 99 | + const popped = this.rightMinHeap.pop(); |
| 100 | + this.leftMinHeap.push(popped * -1); |
| 101 | + } |
| 102 | + |
| 103 | + if (this.leftMinHeap.length - this.rightMinHeap.length > 1) { |
| 104 | + const popped = this.leftMinHeap.pop(); |
| 105 | + this.rightMinHeap.push(popped * -1); |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * @return {number} |
| 111 | + */ |
| 112 | +MedianFinder.prototype.findMedian = function () { |
| 113 | + const len = this.leftMinHeap.length + this.rightMinHeap.length; |
| 114 | + |
| 115 | + if (len % 2 === 0) { |
| 116 | + return (this.leftMinHeap.root * -1 + this.rightMinHeap.root) / 2; |
| 117 | + } |
| 118 | + |
| 119 | + if (this.leftMinHeap.length > this.rightMinHeap.length) { |
| 120 | + return this.leftMinHeap.root * -1; |
| 121 | + } |
| 122 | + |
| 123 | + return this.rightMinHeap.root; |
| 124 | +}; |
| 125 | + |
| 126 | +/** |
| 127 | + * Your MedianFinder object will be instantiated and called as such: |
| 128 | + * var obj = new MedianFinder() |
| 129 | + * obj.addNum(num) |
| 130 | + * var param_2 = obj.findMedian() |
| 131 | + */ |
0 commit comments