码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Find Median from Data Stream

时间:2015-12-28 07:58:35      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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

最大最小堆

复杂度

时间 O(NlogN) 空间 O(N)

思路

维护一个最大堆,一个最小堆。最大堆存的是到目前为止较小的那一半数,最小堆存的是到目前为止较大的那一半数,这样中位数只有可能是堆顶或者堆顶两个数的均值。而维护两个堆的技巧在于判断堆顶数和新来的数的大小关系,还有两个堆的大小关系。我们始终维护MaxHeap>=MinHeap

 

我的solution:

 1 class MedianFinder {
 2     PriorityQueue<Integer> maxHeap;
 3     PriorityQueue<Integer> minHeap;
 4     int count;
 5     
 6     public MedianFinder() {
 7         this.minHeap = new PriorityQueue<Integer>();
 8         this.maxHeap = new PriorityQueue<Integer>(11, new Comparator<Integer>() {
 9             public int compare(Integer i1, Integer i2) {
10                 return i2-i1;
11             }
12         });
13         this.count = 0;
14     }
15     
16 
17     // Adds a number into the data structure.
18     public void addNum(int num) {
19         if (count%2 == 0) {
20             if (count == 0) maxHeap.offer(num);
21             else if (num <= minHeap.peek()) maxHeap.offer(num);
22             else { // num > minHeap.peek()
23                 maxHeap.offer(minHeap.poll());
24                 minHeap.offer(num);
25             }
26         }
27         else { //count%2 == 1
28             if (count == 1) {
29                 if (num < maxHeap.peek()) {
30                     minHeap.offer(maxHeap.poll());
31                     maxHeap.offer(num);
32                 }
33                 else minHeap.offer(num);
34             }
35             else if (num >= maxHeap.peek()) minHeap.offer(num);
36             else { //num < maxHeap.peek()
37                 minHeap.offer(maxHeap.poll());
38                 maxHeap.offer(num);
39             }
40             
41         }
42         count++;
43     }
44 
45     // Returns the median of current data stream
46     public double findMedian() {
47         if (count%2 == 1) return (double)maxHeap.peek();
48         else return (double)(maxHeap.peek() + minHeap.peek())/2.0;
49     }
50 };
51 
52 // Your MedianFinder object will be instantiated and called as such:
53 // MedianFinder mf = new MedianFinder();
54 // mf.addNum(1);
55 // mf.findMedian();

 

简洁方法参考:http://segmentfault.com/a/1190000003709954

 1 class MedianFinder {
 2     
 3     PriorityQueue<Integer> maxheap = new PriorityQueue<Integer>();
 4     PriorityQueue<Integer> minheap = new PriorityQueue<Integer>(Collections.reverseOrder());
 5     
 6     // Adds a number into the data structure.
 7     public void addNum(int num) {
 8         maxheap.offer(num);
 9         minheap.offer(maxheap.poll());
10         if(maxheap.size() < minheap.size()){
11             maxheap.offer(minheap.poll());
12         }
13     }
14 
15     // Returns the median of current data stream
16     public double findMedian() {
17         return maxheap.size() == minheap.size() ? (double)(maxheap.peek() + minheap.peek()) / 2.0 : maxheap.peek();
18     }
19 };

 

Leetcode: Find Median from Data Stream

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/5081437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!