class MedianFinder { constructor() { this.small = [] // max heap this.large = [] // min heap } addNum(num) { if (this.small.length === 0 || num <= -this.small[0]) { this.small.push(-num) this.small.sort((a, b) => a - b) } else { this.large.push(num) this.large.sort((a, b) => a - b) } if (this.small.length > this.large.length + 1) { this.large.push(-this.small.shift()) this.large.sort((a, b) => a - b) } else if (this.large.length > this.small.length) { this.small.push(-this.large.shift()) this.small.sort((a, b) => a - b) } } findMedian() { if (this.small.length > this.large.length) { return -this.small[0] } else { return (-this.small[0] + this.large[0]) / 2 } } } const mf = new MedianFinder() mf.addNum(5) mf.addNum(15) mf.addNum(1) mf.addNum(3) const median = mf.findMedian() console.log(median)
After adding 5 and 15, median is (5+15)/2 = 10. After adding 1, median is 5. After adding 3, numbers are [1,3,5,15], median is (3+5)/2 = 4.
Two heaps split the data stream into lower half (max heap) and upper half (min heap). This allows quick access to middle values for median calculation.
class MedianFinder { constructor() { this.small = [] // max heap this.large = [] // min heap } addNum(num) { if (this.small.length === 0 || num <= -this.small[0]) { this.small.push(-num) this.small.sort((a, b) => a - b) } else { this.large.push(num) this.large.sort((a, b) => a - b) } if (this.small.length > this.large.length + 1) { this.large.push(-this.small.shift()) this.large.sort((a, b) => a - b) } else if (this.large.length > this.small.length) { this.small.push(-this.large.shift()) this.small.sort((a, b) => a - b) } } findMedian() { if (this.small.length > this.large.length) { return -this.small[0] } else { return (-this.small[0] + this.large[0]) / 2 } } } const mf = new MedianFinder() mf.addNum(1) mf.addNum(2) mf.addNum(3) console.log(mf.findMedian())
The code uses arrays and sorts them to simulate heaps but uses shift() which removes the first element, breaking the heap property. This causes incorrect behavior or errors.
Heaps require specific operations like push and pop with heapify. Using shift() and sorting arrays breaks heap properties. Using pop() with a proper heap fixes this.
class MedianFinder { constructor() { this.small = [] // max heap this.large = [] // min heap } addNum(num) { if (this.small.length === 0 || num <= -this.small[0]) { this.small.push(-num) this.small.sort((a, b) => a - b) } else { this.large.push(num) this.large.sort((a, b) => a - b) } if (this.small.length > this.large.length + 1) { this.large.push(-this.small.shift()) this.large.sort((a, b) => a - b) } else if (this.large.length > this.small.length) { this.small.push(-this.large.shift()) this.small.sort((a, b) => a - b) } } findMedian() { if (this.small.length > this.large.length) { return -this.small[0] } else { return (-this.small[0] + this.large[0]) / 2 } } } const mf = new MedianFinder() [10,20,30,40,50,60,70,80,90,100].forEach(n => mf.addNum(n)) console.log(mf.findMedian())
There are 10 numbers, so median is average of 5th and 6th numbers in sorted order: (50 + 60)/2 = 55.
