Complete the code to insert a number into the max heap.
maxHeap.[1](num);We use push to add a new element to the heap.
Complete the code to balance the sizes of the two heaps after insertion.
if (maxHeap.size() > minHeap.size() + [1]) { minHeap.push(maxHeap.top()); maxHeap.pop(); }
We balance heaps when maxHeap size is more than minHeap size by 1.
Fix the error in the code to get the median when heaps are balanced.
return (maxHeap.top() + [1].top()) / 2.0;
The median is average of tops of maxHeap and minHeap, so use minHeap.
Fill both blanks to insert a number into the correct heap based on its value.
if (maxHeap.empty() || num [1] maxHeap.top()) { maxHeap.[2](num); } else { minHeap.push(num); }
If number is less than maxHeap top, insert into maxHeap using push.
Fill all three blanks to return the median correctly based on heap sizes.
if (maxHeap.size() > minHeap.size()) { return maxHeap.[1](); } else if (minHeap.size() > maxHeap.size()) { return minHeap.[2](); } else { return (maxHeap.top() + minHeap.top()) / [3]; }
Return top of the bigger heap or average of both tops divided by 2.0.
