[NeetCode 150] Find Median in a Data Stream

Find Median in a Data Stream

The median is the middle value in a sorted list of integers. For lists of even length, there is no middle value, so the median is the mean of the two middle values.

For example:

For arr = [1,2,3], the median is 2.

For arr = [1,2], the median is (1 + 2) / 2 = 1.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.

Example 1:

复制代码
Input:
["MedianFinder", "addNum", "1", "findMedian", "addNum", "3" "findMedian", "addNum", "2", "findMedian"]

Output:
[null, null, 1.0, null, 2.0, null, 2.0]

Explanation:

MedianFinder medianFinder = new MedianFinder();

medianFinder.addNum(1); // arr = [1]

medianFinder.findMedian(); // return 1.0

medianFinder.addNum(3); // arr = [1, 3]

medianFinder.findMedian(); // return 2.0

medianFinder.addNum(2); // arr[1, 2, 3]

medianFinder.findMedian(); // return 2.0

Constraints:

复制代码
-100,000 <= num <= 100,000

findMedian will only be called after adding at least one integer to the data structure.

Solution

We can divide this ordered list into 2 parts. Maintain the first half via a max-heap and maintain the second half via a min-heap. If we keep the balance between the size of these two heaps, we can guarantee that the median is always at the top of them.

To do this, we need to adjust the size of heaps after each addNum. As we only add 1 number once, we only need to move at most 1 element from one heap to another.

Code

heapq is a good way to realize heap (or say priority queue).

py 复制代码
class MedianFinder:

    def __init__(self):
        self.first = [(100001, -100001)]
        self.second = [(100001, 100001)]
        

    def addNum(self, num: int) -> None:
        first_max = self.first[0][1]
        second_min = self.second[0][1]
        if num <= first_max:
            heapq.heappush(self.first, (-num, num))
        else:
            heapq.heappush(self.second, (num, num))
        if len(self.first) > len(self.second) + 1:
            temp = heapq.heappop(self.first)
            heapq.heappush(self.second, (-temp[0], temp[1]))
        if len(self.second) > len(self.first):
            temp = heapq.heappop(self.second)
            heapq.heappush(self.first, (-temp[0], temp[1]))
        

    def findMedian(self) -> float:
        if len(self.first) == len(self.second):
            return (self.first[0][1]+self.second[0][1])/2
        else:
            return self.first[0][1]
        
        
相关推荐
米粉03058 分钟前
算法图表总结:查找、排序与递归(含 Mermaid 图示)
数据结构·算法·排序算法
人类发明了工具28 分钟前
【优化算法】协方差矩阵自适应进化策略(Covariance Matrix Adaptation Evolution Strategy,CMA-ES)
线性代数·算法·矩阵·cma-es
黑色的山岗在沉睡31 分钟前
LeetCode100.4 移动零
数据结构·算法·leetcode
霖0033 分钟前
PCIe数据采集系统
数据结构·经验分享·单片机·嵌入式硬件·fpga开发·信号处理
_Itachi__36 分钟前
LeetCode 热题 100 114. 二叉树展开为链表
linux·leetcode·链表
敷啊敷衍38 分钟前
深入探索 C++ 中的 string 类:从基础到实践
开发语言·数据结构·c++
方博士AI机器人1 小时前
算法与数据结构 - 二叉树结构入门
数据结构·算法·二叉树
{⌐■_■}1 小时前
【redis】redis常见数据结构及其底层,redis单线程读写效率高于多线程的理解,
数据结构·数据库·redis
-qOVOp-1 小时前
zst-2001 上午题-历年真题 算法(5个内容)
算法
全栈凯哥1 小时前
Java详解LeetCode 热题 100(17):LeetCode 41. 缺失的第一个正数(First Missing Positive)详解
java·算法·leetcode