Complete the code to insert a number into the max heap.
maxHeap.[1](num);We use push to add a new number to the max heap.
Complete the code to balance the heaps when max heap has more elements.
if (maxHeap.size() > minHeap.size() + [1]) { minHeap.push(maxHeap.pop()); }
We balance heaps when max heap size is more than min heap size by 1.
Fix the error in the code to get the median when heaps are equal size.
return (maxHeap.peek() [1] minHeap.peek()) / 2;
Median is average of top elements, so we add then divide by 2.
Fill both blanks to correctly decide which heap to push the new number into.
if (num [1] maxHeap.peek()) { maxHeap.push(num); } else { minHeap.[2](num); }
If the number is less than max heap top, push to max heap; else push to min heap.
Fill all three blanks to correctly rebalance heaps and return the median.
if (maxHeap.size() > minHeap.size() + [1]) { minHeap.push(maxHeap.[2]()); } else if (minHeap.size() > maxHeap.size() + [3]) { maxHeap.push(minHeap.pop()); } if (maxHeap.size() === minHeap.size()) { return (maxHeap.peek() + minHeap.peek()) / 2; } else { return maxHeap.peek(); }
We rebalance when size difference is more than 1 by moving top element using pop, then return median.
