@@ -0,0 +1,2052 @@
+package com.interview.stackqueue;%0A%0Aimport java.util.Collections;%0Aimport java.util.PriorityQueue;%0A%0A/**%0A * Date 03/03/2016%0A * @author Tushar Roy%0A *%0A * Find median in stream of numbers%0A * %0A * https://leetcode.com/problems/find-median-from-data-stream/%0A */%0Aclass MedianFinder %7B%0A%0A    PriorityQueue%3CInteger%3E minPq = new PriorityQueue%3C%3E();%0A    PriorityQueue%3CInteger%3E maxPq = new PriorityQueue%3C%3E();%0A%0A    MedianFinder() %7B%0A        minPq = new PriorityQueue%3C%3E();%0A        maxPq = new PriorityQueue%3C%3E(20, Collections.reverseOrder());%0A    %7D%0A%0A    // Adds a number into the data structure.%0A    public void addNum(int num) %7B%0A        if (maxPq.size() == 0) %7B%0A            maxPq.add(num);%0A            return;%0A        %7D%0A        if (maxPq.size() == minPq.size()) %7B%0A            if (minPq.peek() %3C num) %7B%0A                maxPq.offer(minPq.poll());%0A                minPq.offer(num);%0A            %7D else %7B%0A                maxPq.offer(num);%0A            %7D%0A        %7D else %7B%0A            int toBeOffered = 0;%0A            if (num %3E= maxPq.peek()) %7B%0A                toBeOffered = num;%0A            %7D else %7B%0A                toBeOffered = maxPq.poll();%0A                maxPq.offer(num);%0A            %7D%0A            minPq.offer(toBeOffered);%0A        %7D%0A    %7D%0A    // Returns the median of current data stream%0A    public double findMedian() %7B%0A        if (minPq.size() == maxPq.size()) %7B%0A            return ((double)minPq.peek() + (double)maxPq.peek())/2;%0A        %7D else %7B%0A            return maxPq.peek();%0A        %7D%0A    %7D%0A%0A    public static void main(String args%5B%5D) %7B%0A        MedianFinder mf = new MedianFinder();%0A        mf.addNum(4);%0A        System.out.println(mf.findMedian());%0A        mf.addNum(8);%0A        System.out.println(mf.findMedian());%0A        mf.addNum(2);%0A        System.out.println(mf.findMedian());%0A        mf.addNum(11);%0A        System.out.println(mf.findMedian());%0A        mf.addNum(13);%0A        System.out.println(mf.findMedian());%0A        mf.addNum(14);%0A        System.out.println(mf.findMedian());%0A        mf.addNum(-1);%0A        System.out.println(mf.findMedian());%0A%0A    %7D%0A%7D%0A

Find median in stream of numbers