[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()
。
思路是用两个堆做,一个是最大堆
一个是最小堆
。这个图示应该能清楚说明问题。
我们使用 两个堆(优先队列) 来维护数据流中的前一半和后一半:
maxHeap:最大堆,存储较小的一半(即左边)
minHeap:最小堆,存储较大的一半(即右边)
维护规则:
所有元素分为两部分放入 maxHeap 和 minHeap
maxHeap.size() 总是等于或多于 minHeap.size()(差距不超过 1)
所有 maxHeap 中的元素都小于等于 minHeap 中的元素
中位数的定义:
如果总数为奇数,返回 maxHeap.peek()
如果总数为偶数,返回 (maxHeap.peek() + minHeap.peek()) / 2.0
复杂度
时间O(nlogk) - pq的操作
空间O(n) - pq
代码
Java实现
1 |
|
相关题目
1 |
|