295. Find Median from Data Stream

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

Examples: 

[2,3,4] , the median is 3

[2,3], the median is (2 + 3) / 2 = 2.5

Design a data structure that supports the following two operations:

  • void addNum(int num) - Add a integer number from the data stream to the data structure.
  • double findMedian() - Return the median of all elements so far.

For example:

add(1)
add(2)
findMedian() -> 1.5
add(3) 
findMedian() -> 2

分析:
使用两个heap,一个min heap(root的值最小) and max heap (root的值最大)。因为min-heap的值总是比max-heap大,所以,当我们拿到一个新数的时候,我们需要看它是否比min-heap的root大,如果大,我们应该要添加到min-heap。如果min-heap的size比max-heap的size大于1,我们得把min-heap的最小值转给max-heap。

如果那个数比min-heap的root小,我们就放在max-heap, 但是我们要保证min-heap永远不比max-heap小,所以我们还得check一下。

这里并没有说min-heap size一定要比max-heap size大,其实我们也可以容max-heap比min-heap的size大,我们需要先比较new value和max-heap root的值即可。

 1 public class MedianFinder {
 2     Queue<Integer> minHeap = new PriorityQueue<>();
 3     Queue<Integer> maxHeap = new PriorityQueue<>((x, y) -> y - x);
 4 
 5     public void addNum(int num) {
 6         if (minHeap.isEmpty() || minHeap.peek() <= num) {
 7             minHeap.offer(num);
 8             if (minHeap.size() - maxHeap.size() > 1) {
 9                 maxHeap.offer(minHeap.poll());
10             }
11         } else {
12             maxHeap.offer(num);
13             if (maxHeap.size() > minHeap.size()) {
14                 minHeap.offer(maxHeap.poll());
15             }
16         }
17     }
18 
19     // Returns the median of current data stream
20     public double findMedian() {
21         if (maxHeap.size() == minHeap.size()) {
22             return (maxHeap.peek() + minHeap.peek()) / 2.0;
23         } else {
24             return minHeap.peek();
25         }
26     }
27 }

 

posted @ 2016-07-21 02:46  北叶青藤  阅读(201)  评论(0)    收藏  举报