"""
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. Kosaraju's algorithm

    推荐到我的这篇博客中看完整版的. 该算法用于求解有向图的强连通分量,也就是强连通子图的个数. 算法实现摘自Kosaraju's algorithm - 百度百科: #include <iostr ...

  2. IE浏览器清浮动

    .cfx:after,.cfx:before{content:" ";display:table;}.cfx:after{clear:both;}.cfx{*zoom:1;} 今天 ...

  3. Java入门笔记 03-面向对象(中)

    介绍:这部分内容主要是介绍和总结封装.继承和多态. 一. 封装:把该隐藏的隐藏起来,把该暴露的暴露出来 封装是指将信息隐藏在对象内部,不允许外部程序直接访问对象内部信息,而是通过该类所提供的方法来实现 ...

  4. jupyter更换路径

    初次使用anaconda中自带的jupyter,打开后默认工作路径为C:\Users\Admin(自己的用户名)     需要更换工作路径 更换方式如下: 打开C:\Users\Admin(自己的用户 ...

  5. 如何使html中的元素不被选中

    有时候,为了提高用户的体验度,需要使网页中的部分内容防误操作,不被选中,比如今天的商城项目中的一个细节部分: + —号其实是a标签做的,当连续点击多次,就会使符号被选中,这样感觉起来不太好,于是查找解 ...

  6. ArrayList,LinkedList,vector的区别

    1,Vector.ArrayList都是以类似数组的形式存储在内存中,LinkedList则以链表的形式进行存储. 2.List中的元素有序.允许有重复的元素,Set中的元素无序.不允许有重复元素. ...

  7. Cisco Cat4500系列High CPU故障步骤摘要

    在实际网络环境中,很多时候都会出现设备high CPU的情况,有些时候可能是bug,而某些情况下,也可能是网络中发生了变化,导致了设备出现high CPU情况,这里主要记录一下Cisco Cat450 ...

  8. java中关于类和对象的一些思考

    就这个问题而言 第一种和第二种定义的变量并不是一种形式 前者我们称为原始数据变量 后者我们称为对象变量 这两种变量的创建方式,定义方式,使用方式都有着很多不同 需要引起注意. 在java中,有着基本的 ...

  9. 【转】ssh 远程执行命令

    原文:https://blog.csdn.net/liuxiao723846/article/details/82667482 SSH 是 Linux 下进行远程连接的基本工具,不光可以登录,也可以远 ...

  10. C++ STL之映射map的使⽤

    写在最前面:本文摘录于柳神笔记: map 是键值对,⽐如⼀个⼈名对应⼀个学号,就可以定义⼀个字符串 string 类型的⼈名为“键”,学 号 int 类型为“值”,如 map<string, i ...