从数据流中查找中值
“从数据流中查找中值”问题的详细指南和 Python 实现。
1. 学习
“从数据流中查找中值”问题是堆/优先级队列部分的一个关键挑战。
此实现重点关注 Python 中的硬级逻辑。
在我们提供的解决方案中,我们优先考虑技术准确性和代码可读性。
2. Real-World Applications
3. Visual Intuition
可视化从数据流查找中值的逻辑流程。
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
仔细阅读“从数据流中查找中值”的问题陈述。
2. Formulate brute force
起草一个简单的迭代解决方案。
3. Identify inefficiency
寻找冗余计算。
4. Optimize search path
使用散列或排序来加速该过程。
5. Final Implementation
清理生产标准代码。
问题陈述
中位数是有序整数列表中的中间值。如果列表的大小是偶数,则没有中间值,中位数是中间两个值的平均值。
实现 MedianFinder 类:
- MedianFinder() 初始化 MedianFinder 对象。
- addNum(num: int) 将数据流中的整数 num 添加到数据结构中。
- findMedian() -> float 返回到目前为止所有元素的中位数。
输入是操作和参数的列表。实现一个返回结果列表的函数 medianFinder(operations: list, arguments: list) -> list (构造函数/addNum 为 None,findMedian 为 float)。
- •-10^5 <= num <= 10^5
- •There will be at least one element in the data structure before calling findMedian
- •At most 5 * 10^4 calls will be made to addNum and findMedian
示例
operations = ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"], arguments = [[], [1], [2], [], [3], []]
[None, None, None, 1.5, None, 2.0]
Initialize. Add 1, 2. Median is 1.5. Add 3. Median is 2.0.
Need a Hint?
Edge Cases to Watch
- 空输入结构
- 单元素输入
- 大数值范围
准备好解决了吗?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
面试见解和变化
复杂性分析分解
为什么时间: Directly evaluates all possibilities.
为什么选择太空: Uses standard local memory.
为什么时间: Optimized paths reduce total operations.
为什么选择太空: May trade memory for speed.
优化解决方案Python代码
优化解决方案Python代码
import heapq
class MedianFinderOpt:
def __init__(self):
self.small, self.large = [], []
def addNum(self, num):
heapq.heappush(self.small, -1 * num)
if self.small and self.large and (-1 * self.small[0]) > self.large[0]:
val = -1 * heapq.heappop(self.small)
heapq.heappush(self.large, val)
if len(self.small) > len(self.large) + 1:
val = -1 * heapq.heappop(self.small)
heapq.heappush(self.large, val)
if len(self.large) > len(self.small) + 1:
val = heapq.heappop(self.large)
heapq.heappush(self.small, -1 * val)
def findMedian(self):
if len(self.small) > len(self.large): return -1 * self.small[0]
if len(self.large) > len(self.small): return self.large[0]
return (-1 * self.small[0] + self.large[0]) / 2暴力破解代码(剧透保护)
暴力破解代码(剧透保护)
class MedianFinderBrute:
def __init__(self):
self.nums = []
def addNum(self, num):
self.nums.append(num)
def findMedian(self):
self.nums.sort()
n = len(self.nums)
if n % 2: return self.nums[n//2]
return (self.nums[n//2-1] + self.nums[n//2]) / 2Algorithm Pattern Checklist
When dealing with Heap / Priority Queue data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推荐的 Python 资源
通过相关的交互式教程、备忘单和代码比较来扩展您的知识。
Python 循环:For 和 While 循环解释
了解如何使用 Python 循环来迭代数据。通过交互式示例掌握 for 循环、while 循环、break、continue 和循环最佳实践。
如何在 Python 中删除列表中的重复项
了解如何从 Python 列表中删除重复项,同时保持或忽略顺序。比较集合转换、字典键和循环方法。
Python 集合和数据结构备忘单
Python 集合模块和本机数据结构的完整指南。学习列表、字典、集合、元组、双端队列和命名元组。
Python 与 JavaScript:哪种编程语言最好?
Python 和 JavaScript 的全面比较。探索语法差异、性能、用例(后端与前端)和编码示例。