"""
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
"""
"""
用一个大顶堆和一个小顶堆来维护数据,
每次每个数进来,先把它丢进小顶堆,然后把小顶堆的堆顶丢进大顶堆,
调整两个堆,使得size 差最大为1。
这么搞的好处在于,小顶堆是数据流里前一半大的数,大顶堆是数据流里后一半的大的数,
而且小顶堆的size一定 >= 大顶堆的size,
小顶堆的堆顶M是小顶堆里最小的数,大顶堆的堆顶N是大顶堆里最大的数,
如果两个堆的size相同,那么中位数就是return (M + N) / 2.0
否则,return M / 1.0。
注意python没有大顶堆,所以放进大顶堆的数乘了-1, 取出来的时候也要记得 * -1
传送门:https://blog.csdn.net/qq_32424059/article/details/90346347
""" from heapq import *
class MedianFinder: def __init__(self):
"""
initialize your data structure here.
"""
self.max_h = list()
self.min_h = list()
heapify(self.max_h)
heapify(self.min_h) def addNum(self, num: int) -> None:
heappush(self.min_h, num)
heappush(self.max_h, -heappop(self.min_h))
if len(self.max_h) > len(self.min_h):
heappush(self.min_h, -heappop(self.max_h)) def findMedian(self) -> float:
max_len = len(self.max_h)
min_len = len(self.min_h)
if max_len == min_len: # 有两个候选中位数
return (self.min_h[0] + -self.max_h[0]) / 2.
else: # 小顶堆的size 一定 >= 大顶堆的size,所以答案就是小顶堆的堆顶
return self.min_h[0] / 1.

leetcode295 Find Median from Data Stream的更多相关文章

  1. [LeetCode] Find Median from Data Stream

    Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...

  2. [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)

    295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...

  3. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  4. [Swift]LeetCode295. 数据流的中位数 | 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 ...

  5. [LeetCode] 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 ...

  6. Find Median from Data Stream

    常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

  7. 数据结构与算法(1)支线任务8——Find Median from Data Stream

    题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...

  8. LeetCode——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 ...

  9. 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 ...

随机推荐

  1. 基于Ubuntu和基于Debian的Linux Mint 20新特性一览

    导读 Linux Mint 20 将基于 Ubuntu 20.04 LTS,同时,其 LMDE(Linux Mint Debian Edition,Debian 版本)4 也将到来,LMDE 版本基于 ...

  2. 洛谷P1131 时态同步

    题意: 给一个n点的树,每条边都有边权,问从根出发需要增加多少长度,使得最终的儿子到根的距离是一样的 思路: 上来一个思路wa了3次,看完题解之后,又一次豁然开朗…… orz #include< ...

  3. nginx+keepalived实现主从高可用

    设备: 主:192.168.200.122 从:192.168.200.124 安装:两台都分别安装nginx以及keepalived 两台机步骤一致一主一从 安装nginx的依赖包 [root@lo ...

  4. java比较时间的方法

    一.通过compareTo Date date = new Date(1576118709574L); Date date1 = new Date(1576118709574L); Date date ...

  5. 【笔记5-购物车及地址模块】从0开始 独立完成企业级Java电商网站开发(服务端)

    购物车模块 数据库表设计 购物车表 CREATE TABLE mmall_ cart ( 'id' int(11) NOT NULL AUTO_ INCREMENT, 'user_ id' int(1 ...

  6. 微服务架构中的Redis

    了解如何将Redis与Spring Cloud和Spring Data一起使用以提供配置服务器,消息代理和数据库. Redis可以广泛用于微服务架构中.它可能是少数流行的软件解决方案之一,你的应用程序 ...

  7. 【原】简单shell练习(五)

    1.查询一个文件中某个单词出现的次数 # grep -o 'ts' ./test/txt |wc -l 2.统计当前文件夹下文件的个数 ls -l |grep "^-"|wc -l ...

  8. 中国电信与小米成立5G联合创新实验室

    导读 中国电信与小米成立5G联合创新实验室 近日,在中国电信战略与创新研究院,小米与中国电信共同发起的5G联合创新实验室正式揭牌成立.双方将充分发挥技术.网络.产品和生态的优势,围绕“5G+AIoT” ...

  9. 洛谷 T8088 RQY的舞会

    嗯... 题目链接:https://www.luogu.org/problem/T8088 这道题很好想,我想的是维护两个小根堆(当然可以用数组模拟) 然后从堆顶开始,如果两个元素的差小于1,则cnt ...

  10. Windows Server 2008 R2文件服务器升级到Windows Server 2016

    Windows Server 2008 R2文件服务器升级到Windows Server 2016 用户单位有2台Windows Server 2008 R2的文件服务器,已经加入到域.域服务器另有两 ...