[LeetCode] 295. Find Median from Data Stream

 

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.

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

Constraints:

  • -105 <= num <= 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.

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?

找出数据流中的中位数。

中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。

例如,

[2,3,4] 的中位数是 3

[2,3] 的中位数是 (2 + 3) / 2 = 2.5

设计一个支持以下两种操作的数据结构:

void addNum(int num) - 从数据流中添加一个整数到数据结构中。
double findMedian() - 返回目前所有元素的中位数。
示例:

addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2
进阶:

如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法?
如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法?

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-median-from-data-stream
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意是给一个数据流,请根据题意设计几个函数。

其中需要实现的函数是addNum()和findMedian()。

思路是用两个堆做,一个是最大堆一个是最小堆。从一开始放入元素的时候就要始终保持两个堆的元素数量要一样,或者最大堆的元素多一个。如果放入了偶数个元素,那么最小堆和最大堆的顶端元素的平均值是中位数,如果放入了奇数个元素,则最大堆的顶端元素是中位数。下图例子应该能清楚说明问题(引用)。

 

 

时间O(nlogk) - pq的操作

空间O(n) - pq

Java实现,同时注意pq的写法,参见这个帖子

 1 class MedianFinder {
 2     private PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
 3     private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
 4     private boolean even = true;
 5 
 6     /** initialize your data structure here. */
 7     public MedianFinder() {
 8 
 9     }
10 
11     public void addNum(int num) {
12         if (even) {
13             minHeap.offer(num);
14             maxHeap.offer(minHeap.poll());
15         } else {
16             maxHeap.offer(num);
17             minHeap.offer(maxHeap.poll());
18         }
19         even = !even;
20     }
21 
22     public double findMedian() {
23         if (even) {
24             return (maxHeap.peek() + minHeap.peek()) / 2.0;
25         } else {
26             return maxHeap.peek();
27         }
28     }
29 }
30 
31 /**
32  * Your MedianFinder object will be instantiated and called as such:
33  * MedianFinder obj = new MedianFinder(); obj.addNum(num); double param_2 =
34  * obj.findMedian();
35  */

 

相关题目

295. Find Median from Data Stream

480. Sliding Window Median

LeetCode 题目总结

posted @ 2020-03-10 07:06  CNoodle  阅读(297)  评论(0编辑  收藏  举报