The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.


	For example, for arr = [2,3,4], the median is 3.
	For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.


Implement the MedianFinder class:


	MedianFinder() initializes the MedianFinder object.
	void addNum(int num) adds the integer num from the data stream to the data structure.
	double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.


&nbsp;
Example 1:

Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]

Explanation
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1);    // arr = [1]
medianFinder.addNum(2);    // arr = [1, 2]
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
medianFinder.addNum(3);    // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0


&nbsp;
Constraints:


	-105 &lt;= num &lt;= 105
	There will be at least one element in the data structure before calling findMedian.
	At most 5 * 104 calls will be made to addNum and findMedian.


&nbsp;
Follow up:


	If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
	If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?


class MedianFinder:

    def __init__(self):
        self.min_hp = []
        self.max_hp = []
        
    def addNum(self, num: int) -> None:
        if len(self.min_hp) == len(self.max_hp):
            if len(self.max_hp) and num<-self.max_hp[0]:
                cur = -heapq.heappop(self.max_hp)
                heapq.heappush(self.max_hp, -num)
                heapq.heappush(self.min_hp, cur)
            else:
                heapq.heappush(self.min_hp, num)
        else:
            if num>self.min_hp[0]:
                cur = heapq.heappop(self.min_hp)
                heapq.heappush(self.min_hp, num)
                heapq.heappush(self.max_hp, -cur)
            else:
                heapq.heappush(self.max_hp, -num)
        
    def findMedian(self) -> float:
        if len(self.min_hp) == len(self.max_hp):
            return (self.min_hp[0] + -self.max_hp[0]) /2
        else:
            return self.min_hp[0]
class MedianFinder {

    PriorityQueue maxHeap;
    PriorityQueue minHeap;

    public MedianFinder() {
        maxHeap= new PriorityQueue<Integer>((a,b)->b-a);
        minHeap= new PriorityQueue<Integer>();
    }

    public void addNum(int num) {

        //Pushing
        if ( maxHeap.isEmpty() || ((int)maxHeap.peek() > num) ){
            maxHeap.offer(num);
        }
        else{
            minHeap.offer(num);
        }

        //Balancing
        if ( maxHeap.size() > minHeap.size()+ 1){
             minHeap.offer(maxHeap.peek());
             maxHeap.poll();
        }
        else if (minHeap.size() > maxHeap.size()+ 1 ){
             maxHeap.offer(minHeap.peek());
             minHeap.poll();
        }

    }

    public double findMedian() {

        //Evaluating Median
        if ( maxHeap.size() == minHeap.size() ){ // Even Number
            return ((int)maxHeap.peek()+ (int)minHeap.peek())/2.0;
        }
        else{ //Odd Number
             if ( maxHeap.size() > minHeap.size()){
                 return (int)maxHeap.peek()+ 0.0;
             }
            else{ // minHeap.size() > maxHeap.size()
                 return (int)minHeap.peek()+ 0.0;
            }
        }
    }
}
class MedianFinder {
public:
    /* Implemented @StefanPochmann's Incridible Idea */
    priority_queue<long long> small, large;
    MedianFinder() {
        
    }
    
    void addNum(int num) {
        small.push(num);          // cool three step trick
        large.push(-small.top());
        small.pop();
        while(small.size() < large.size()){
            small.push(-large.top());
            large.pop();
        }
    }
    
    double findMedian() {
        return small.size() > large.size()
            ? small.top()
            : (small.top() - large.top())/2.0;
    }
};
var MedianFinder = function() {
    this.left = new MaxPriorityQueue();
  this.right = new MinPriorityQueue();
};

/**
 * @param {number} num
 * @return {void}
 */
MedianFinder.prototype.addNum = function(num) {
let { right, left } = this
  if (right.size() > 0 && num > right.front().element) {
    right.enqueue(num)
  } else {
    left.enqueue(num)
  }

  if (Math.abs(left.size() - right.size()) == 2) {
    if (left.size() > right.size()) {
      right.enqueue(left.dequeue().element)
    } else {
      left.enqueue(right.dequeue().element)
    }
  }
};

/**
 * @return {number}
 */
MedianFinder.prototype.findMedian = function() {
  let { left, right } = this
  if (left.size() > right.size()) {
    return left.front().element
  } else if(right.size() > left.size()) {
    return right.front().element
  } else{
      // get the sum of all
      return (left.front().element + right.front().element) / 2
  }
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * var obj = new MedianFinder()
 * obj.addNum(num)
 * var param_2 = obj.findMedian()
 */
Find Median from Data Stream