[LeetCode] 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.
For example,
[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.
Example:
addNum(1) addNum(2) findMedian() -> 1.5 addNum(3) findMedian() -> 2
此题要找出数据流的中位数,数据流由无序整数组成,并且数据流是在变化的。中位数的定义是,如果数字个数为偶数,中位数就是中间两个数的平均数,如果是奇数,中位数是中间那个数字。
解法1: 最简单的想法是,每进来一个数字后,插入排序整个数组,保持数组是有序的,然后分奇偶的情况找到中位数。此方法不高效。
解法2: 堆Heap,将进来的数据流分成大小两个堆,分别保存堆的前半部分和后半部分,大堆是最小数字在堆顶,小堆是最大数在堆顶。取中间值时,根据两个堆的数字个数,如果个数相同,就各取堆顶数字取平均数就是中间数,如果有一个堆数字多1,中间数就是这个堆顶数字。
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class MedianFinder { PriorityQueue<Integer> maxHeap; //lower half PriorityQueue<Integer> minHeap; //higher half public MedianFinder(){ maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder()); minHeap = new PriorityQueue<Integer>(); } // Adds a number into the data structure. public void addNum( int num) { maxHeap.offer(num); minHeap.offer(maxHeap.poll()); if (maxHeap.size() < minHeap.size()){ maxHeap.offer(minHeap.poll()); } } // Returns the median of current data stream public double findMedian() { if (maxHeap.size()==minHeap.size()){ return ( double )(maxHeap.peek()+(minHeap.peek()))/ 2 ; } else { return maxHeap.peek(); } } } |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | from heapq import heappush, heappop class MedianFinder: def __init__( self ): """ Initialize your data structure here. """ self .__max_heap = [] self .__min_heap = [] def addNum( self , num): """ Adds a num into the data structure. :type num: int :rtype: void """ # Balance smaller half and larger half. if not self .__max_heap or num > - self .__max_heap[ 0 ]: heappush( self .__min_heap, num) if len ( self .__min_heap) > len ( self .__max_heap) + 1 : heappush( self .__max_heap, - heappop( self .__min_heap)) else : heappush( self .__max_heap, - num) if len ( self .__max_heap) > len ( self .__min_heap): heappush( self .__min_heap, - heappop( self .__max_heap)) def findMedian( self ): """ Returns the median of current data stream :rtype: float """ return ( - self .__max_heap[ 0 ] + self .__min_heap[ 0 ]) / 2.0 \ if len ( self .__min_heap) = = len ( self .__max_heap) \ else self .__min_heap[ 0 ] |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class MedianFinder { public : // Adds a number into the data structure. void addNum( int num) { small.push(num); large.push(-small.top()); small.pop(); if (small.size() < large.size()) { small.push(-large.top()); large.pop(); } } // Returns the median of current data stream double findMedian() { return small.size() > large.size() ? small.top() : 0.5 *(small.top() - large.top()); } private : priority_queue< long > small, large; }; |
All LeetCode Questions List 题目汇总
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架