[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]
        
        
相关推荐
ZZZ_O^O9 分钟前
【贪心算法第五弹——300.最长递增子序列】
c++·学习·算法·leetcode·贪心算法
码农多耕地呗11 分钟前
哈希表—acwing
数据结构·散列表
呼啦啦啦啦啦啦啦啦12 分钟前
刷题日常(移动零,盛最多水的容器,三数之和,无重复字符的最长子串)
算法·双指针·滑动窗口
Koishi_TvT14 分钟前
蓝桥杯c++算法秒杀【6】之动态规划【下】(数字三角形、砝码称重(背包问题)、括号序列、异或三角:::非常典型的必刷例题!!!)
c语言·c++·算法·性能优化·蓝桥杯·动态规划·c
孤独且没人爱的纸鹤14 分钟前
C++ 二叉搜索树(Binary Search Tree, BST)深度解析与全面指南:从基础概念到高级应用、算法优化及实战案例
c语言·数据结构·c++·算法
YuanLiu_2271 小时前
代码随想录算法训练营第十三天(递归遍历;迭代遍历;统一迭代;层序遍历)
java·数据结构·笔记·算法·leetcode
闻缺陷则喜何志丹1 小时前
【C++动态规划】1411. 给 N x 3 网格图涂色的方案数|1844
c++·算法·动态规划·力扣·网格·数目·涂色
仙俊红1 小时前
快速运行openMMOCR
深度学习·算法
-Max-静-1 小时前
Paddle Inference部署推理(十八)
人工智能·windows·深度学习·算法·paddle·推理 部署
f狐0狸x2 小时前
【数据结构实战篇】用C语言实现你的私有队列
c语言·数据结构·链表··队列