Heaps: Find the Running Median

  • + 11 comments

    I'll give you guys a hint. You need a min-heap and a max-heap.

    Min-heap will contain the maximum half of the numbers from the array. Max-heap will contain the minimum half of the numbers from the array.

    So the top value from the min-heap will be the minimum number from the max half of the array. The top value from the max-heap will be the maximum number from the min half of the array.

    So you pretty much have the middle values of the array, therefore can calculate the median. So think about it, what can you change to make it work with odd number of values?