|
| 1 | +class MedianFinder { |
| 2 | + constructor() { |
| 3 | + this.minHeap = new MinHeap(); // large set [4,5,6] |
| 4 | + this.maxHeap = new MaxHeap(); // smaller set [1,2,3] |
| 5 | + } |
| 6 | + |
| 7 | + addNum(num) { |
| 8 | + console.log(`num:${num}, peek:`) |
| 9 | + if (this.maxHeap.isEmpty() || num < this.maxHeap.peek()) { |
| 10 | + this.maxHeap.insert(num); |
| 11 | + } else { |
| 12 | + this.minHeap.insert(num); |
| 13 | + } |
| 14 | + |
| 15 | + console.log(this.maxHeap) |
| 16 | + console.log(this.minHeap) |
| 17 | + |
| 18 | + if (this.maxHeap.size() - this.minHeap.size() > 1) { |
| 19 | + console.log("reordering max") |
| 20 | + this.minHeap.insert(this.maxHeap.remove()); |
| 21 | + } else if (this.minHeap.size() - this.maxHeap.size() > 1) { |
| 22 | + console.log("reordering min") |
| 23 | + this.maxHeap.insert(this.minHeap.remove()); |
| 24 | + } |
| 25 | + |
| 26 | + console.log(this.maxHeap) |
| 27 | + console.log(this.minHeap) |
| 28 | + } |
| 29 | + |
| 30 | + findMedian() { |
| 31 | + if (this.maxHeap.size() === this.minHeap.size()) { |
| 32 | + return (this.maxHeap.peek() + this.minHeap.peek()) / 2; |
| 33 | + } else if (this.maxHeap.size() > this.minHeap.size()) { |
| 34 | + return this.maxHeap.peek(); |
| 35 | + } else { |
| 36 | + return this.minHeap.peek(); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// MinHeap class implementation |
| 42 | +class MinHeap { |
| 43 | + constructor() { |
| 44 | + this.heap = []; |
| 45 | + } |
| 46 | + |
| 47 | + // ... (implementation details of MinHeap) |
| 48 | + |
| 49 | + // Additional methods used by MedianFinder class |
| 50 | + size() { |
| 51 | + return this.heap.length; |
| 52 | + } |
| 53 | + |
| 54 | + peek() { |
| 55 | + if (this.heap.length === 0) { |
| 56 | + return false; |
| 57 | + throw new Error("Heap is empty."); |
| 58 | + } |
| 59 | + return this.heap[0]; |
| 60 | + } |
| 61 | + |
| 62 | + remove() { |
| 63 | + if (this.heap.length === 0) { |
| 64 | + return false; |
| 65 | + throw new Error("Heap is empty."); |
| 66 | + } |
| 67 | + const minValue = this.heap[0]; |
| 68 | + this.heap[0] = this.heap.pop(); |
| 69 | + this.heapifyDown(); |
| 70 | + return minValue; |
| 71 | + } |
| 72 | + |
| 73 | + insert(value) { |
| 74 | + this.heap.push(value); |
| 75 | + this.heapifyUp(); |
| 76 | + } |
| 77 | + |
| 78 | + isEmpty() { |
| 79 | + return this.heap.length === 0; |
| 80 | + } |
| 81 | + |
| 82 | + heapifyUp() { |
| 83 | + let index = this.heap.length - 1; |
| 84 | + while (this.hasParent(index) && this.parent(index) > this.heap[index]) { |
| 85 | + const parentIndex = this.getParentIndex(index); |
| 86 | + this.swap(parentIndex, index); |
| 87 | + index = parentIndex; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + heapifyDown() { |
| 92 | + let index = 0; |
| 93 | + while (this.hasLeftChild(index)) { |
| 94 | + let smallerChildIndex = this.getLeftChildIndex(index); |
| 95 | + if ( |
| 96 | + this.hasRightChild(index) && |
| 97 | + this.rightChild(index) < this.leftChild(index) |
| 98 | + ) { |
| 99 | + smallerChildIndex = this.getRightChildIndex(index); |
| 100 | + } |
| 101 | + |
| 102 | + if (this.heap[index] < this.heap[smallerChildIndex]) { |
| 103 | + break; |
| 104 | + } else { |
| 105 | + this.swap(index, smallerChildIndex); |
| 106 | + } |
| 107 | + index = smallerChildIndex; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Helper methods |
| 112 | + getLeftChildIndex(parentIndex) { |
| 113 | + return 2 * parentIndex + 1; |
| 114 | + } |
| 115 | + |
| 116 | + getRightChildIndex(parentIndex) { |
| 117 | + return 2 * parentIndex + 2; |
| 118 | + } |
| 119 | + |
| 120 | + getParentIndex(childIndex) { |
| 121 | + return Math.floor((childIndex - 1) / 2); |
| 122 | + } |
| 123 | + |
| 124 | + hasLeftChild(index) { |
| 125 | + return this.getLeftChildIndex(index) < this.heap.length; |
| 126 | + } |
| 127 | + |
| 128 | + hasRightChild(index) { |
| 129 | + return this.getRightChildIndex(index) < this.heap.length; |
| 130 | + } |
| 131 | + |
| 132 | + hasParent(index) { |
| 133 | + return this.getParentIndex(index) >= 0; |
| 134 | + } |
| 135 | + |
| 136 | + leftChild(index) { |
| 137 | + return this.heap[this.getLeftChildIndex(index)]; |
| 138 | + } |
| 139 | + |
| 140 | + rightChild(index) { |
| 141 | + return this.heap[this.getRightChildIndex(index)]; |
| 142 | + } |
| 143 | + |
| 144 | + parent(index) { |
| 145 | + return this.heap[this.getParentIndex(index)]; |
| 146 | + } |
| 147 | + |
| 148 | + swap(index1, index2) { |
| 149 | + [this.heap[index1], this.heap[index2]] = [ |
| 150 | + this.heap[index2], |
| 151 | + this.heap[index1], |
| 152 | + ]; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// MaxHeap class implementation |
| 157 | +class MaxHeap { |
| 158 | + constructor() { |
| 159 | + this.heap = []; |
| 160 | + } |
| 161 | + |
| 162 | + // ... (implementation details of MaxHeap) |
| 163 | + |
| 164 | + // Additional methods used by MedianFinder class |
| 165 | + size() { |
| 166 | + return this.heap.length; |
| 167 | + } |
| 168 | + |
| 169 | + peek() { |
| 170 | + if (this.heap.length === 0) { |
| 171 | + return false; |
| 172 | + throw new Error("Heap is empty."); |
| 173 | + } |
| 174 | + return this.heap[0]; |
| 175 | + } |
| 176 | + |
| 177 | + remove() { |
| 178 | + if (this.heap.length === 0) { |
| 179 | + return false; |
| 180 | + throw new Error("Heap is empty."); |
| 181 | + } |
| 182 | + const maxValue = this.heap[0]; |
| 183 | + this.heap[0] = this.heap.pop(); |
| 184 | + this.heapifyDown(); |
| 185 | + return maxValue; |
| 186 | + } |
| 187 | + |
| 188 | + insert(value) { |
| 189 | + this.heap.push(value); |
| 190 | + this.heapifyUp(); |
| 191 | + } |
| 192 | + |
| 193 | + isEmpty() { |
| 194 | + return this.heap.length === 0; |
| 195 | + } |
| 196 | + |
| 197 | + heapifyUp() { |
| 198 | + let index = this.heap.length - 1; |
| 199 | + while (this.hasParent(index) && this.parent(index) < this.heap[index]) { |
| 200 | + const parentIndex = this.getParentIndex(index); |
| 201 | + this.swap(parentIndex, index); |
| 202 | + index = parentIndex; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + heapifyDown() { |
| 207 | + let index = 0; |
| 208 | + while (this.hasLeftChild(index)) { |
| 209 | + let largerChildIndex = this.getLeftChildIndex(index); |
| 210 | + if ( |
| 211 | + this.hasRightChild(index) && |
| 212 | + this.rightChild(index) > this.leftChild(index) |
| 213 | + ) { |
| 214 | + largerChildIndex = this.getRightChildIndex(index); |
| 215 | + } |
| 216 | + |
| 217 | + if (this.heap[index] > this.heap[largerChildIndex]) { |
| 218 | + break; |
| 219 | + } else { |
| 220 | + this.swap(index, largerChildIndex); |
| 221 | + } |
| 222 | + index = largerChildIndex; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + // Helper methods |
| 227 | + getLeftChildIndex(parentIndex) { |
| 228 | + return 2 * parentIndex + 1; |
| 229 | + } |
| 230 | + |
| 231 | + getRightChildIndex(parentIndex) { |
| 232 | + return 2 * parentIndex + 2; |
| 233 | + } |
| 234 | + |
| 235 | + getParentIndex(childIndex) { |
| 236 | + return Math.floor((childIndex - 1) / 2); |
| 237 | + } |
| 238 | + |
| 239 | + hasLeftChild(index) { |
| 240 | + return this.getLeftChildIndex(index) < this.heap.length; |
| 241 | + } |
| 242 | + |
| 243 | + hasRightChild(index) { |
| 244 | + return this.getRightChildIndex(index) < this.heap.length; |
| 245 | + } |
| 246 | + |
| 247 | + hasParent(index) { |
| 248 | + return this.getParentIndex(index) >= 0; |
| 249 | + } |
| 250 | + |
| 251 | + leftChild(index) { |
| 252 | + return this.heap[this.getLeftChildIndex(index)]; |
| 253 | + } |
| 254 | + |
| 255 | + rightChild(index) { |
| 256 | + return this.heap[this.getRightChildIndex(index)]; |
| 257 | + } |
| 258 | + |
| 259 | + parent(index) { |
| 260 | + return this.heap[this.getParentIndex(index)]; |
| 261 | + } |
| 262 | + |
| 263 | + swap(index1, index2) { |
| 264 | + [this.heap[index1], this.heap[index2]] = [ |
| 265 | + this.heap[index2], |
| 266 | + this.heap[index1], |
| 267 | + ]; |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +var obj = new MedianFinder(); |
| 272 | +// obj.addNum(10); |
| 273 | +// obj.addNum(2); |
| 274 | +// obj.addNum(13); |
| 275 | +obj.addNum(3); |
| 276 | +obj.addNum(1); |
| 277 | +obj.addNum(2); |
| 278 | +var param_2 = obj.findMedian(); |
| 279 | +console.log(`p2: ${param_2}`) |
| 280 | +// obj.addNum(3); |
| 281 | +// obj.addNum(8); |
| 282 | +// obj.addNum(6); |
| 283 | +obj.addNum(7); |
| 284 | +obj.addNum(4); |
| 285 | +obj.addNum(5); |
| 286 | +obj.addNum(6); |
| 287 | +obj.addNum(9); |
| 288 | + |
| 289 | +var param_3 = obj.findMedian(); |
| 290 | +console.log(`p3: ${param_3}`) |
0 commit comments