一、題目要求
中位數是有序整數列表中的中間值。如果列表的大小是偶數,則沒有中間值,中位數是兩個中間值的平均值。
例如 arr = [2,3,4] 的中位數是 3 。
例如 arr = [2,3] 的中位數是 (2 + 3) / 2 = 2.5 。
實現 MedianFinder 類:
MedianFinder() 初始化 MedianFinder 對象。
void addNum(int num) 將數據流中的整數 num 添加到數據結構中。
double findMedian() 返回到目前為止所有元素的中位數。與實際答案相差 10-5 以內的答案將被接受。
示例 1:
輸入
[“MedianFinder”, “addNum”, “addNum”, “findMedian”, “addNum”, “findMedian”]
[[], [1], [2], [], [3], []]
輸出
[null, null, null, 1.5, null, 2.0]
解釋
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1); // arr = [1]
medianFinder.addNum(2); // arr = [1, 2]
medianFinder.findMedian(); // 返回 1.5 ((1 + 2) / 2)
medianFinder.addNum(3); // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0
提示:
-105 <= num <= 105
在調用 findMedian 之前,數據結構中至少有一個元素
最多 5 * 104 次調用 addNum 和 findMedian
二、實現代碼
原理:使用了兩個堆存儲數據,一個最大堆用于存儲較小的一半元素,另一個最小堆用于存儲較大的一半元素,然后根據堆頂元素計算得到中位數。
1. Java
class MedianFinder {private PriorityQueue<Integer> low;private PriorityQueue<Integer> high;public MedianFinder() {// Max-heap to store the smaller half elementslow = new PriorityQueue<>((a, b) -> b - a);// Min-heap to store the larger half elementshigh = new PriorityQueue<>();}public void addNum(int num) {low.offer(num);high.offer(low.poll());if (low.size() < high.size()) {low.offer(high.poll());}}public double findMedian() {if (low.size() > high.size()) {return low.peek();} else {return (low.peek() + high.peek()) / 2.0;}}
}
2. C++
class MedianFinder {
private:priority_queue<int> low; // Max-heappriority_queue<int, vector<int>, greater<int>> high; // Min-heappublic:MedianFinder() { }void addNum(int num) {low.push(num);high.push(low.top());low.pop();if (low.size() < high.size()) {low.push(high.top());high.pop();}}double findMedian() {if (low.size() > high.size()) {return low.top();} else {return (low.top() + high.top()) / 2.0;}}
};
3. Python3
class MedianFinder:def __init__(self):self.low = [] # max-heap (inverted min-heap)self.high = [] # min-heapdef addNum(self, num: int) -> None:heapq.heappush(self.low, -num)heapq.heappush(self.high, -heapq.heappop(self.low))if len(self.low) < len(self.high):heapq.heappush(self.low, -heapq.heappop(self.high))def findMedian(self) -> float:if len(self.low) > len(self.high):return -self.low[0]else:return (-self.low[0] + self.high[0]) / 2.0
注:如果四python會出錯,只能是python3