Find Median from Data Stream

要点:

  • 基本框架:两个heap:large,small把所有数二分。一个新的element。目标:维持heap中的元素个数相同。错误理解:新元素不是任意可以push到large里的,其值可能应该属于small。所以要2步push/pop,之后判断大小。
  • large表示大的那边,small表示小的那边。固定:large可以多一个元素
  • => 新元素:先push large,然后从large中pop到small。所以small是增加的。如果small和large原来一样,需要重新把中间元素push回large。这样可以不用考虑奇偶在branch,好记。
    • 但是仍然要记住even/odd的关系:
    • odd/even表示当前heap里的,不包括新element
    • 开始是even,所以small里面push再到pop到large里,odd相反。
  • 都用minHeap,只需要pop之后取反push到另一个即可。而large的minHeap是自然的。
  • -2^31 negate还是-2^31,所以用long

https://repl.it/CeiU/1

错误点:

  • 注意self.small中的元素是反的,所以要减去
# 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.

# Examples:
# [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.
# For example: # add(1)
# add(2)
# findMedian() -> 1.5
# add(3)
# findMedian() -> 2
# Credits:
# Special thanks to @Louis1992 for adding this problem and creating all test cases. # Hide Company Tags Google
# Hide Tags Heap Design from heapq import heappush, heappop
class MedianFinder:
def __init__(self):
"""
Initialize your data structure here.
"""
self.large = []
self.small = [] def addNum(self, num):
"""
Adds a num into the data structure.
:type num: int
:rtype: void
"""
heappush(self.large, num)
heappush(self.small, -heappop(self.large))
if len(self.large)<len(self.small):
heappush(self.large, -heappop(self.small)) def findMedian(self):
"""
Returns the median of current data stream
:rtype: float
"""
if (len(self.small)+len(self.large)) & 1:
return self.large[0]/1.0
else:
return (self.large[0]-self.small[0])/2.0 # error: should be - not + b/c in small is negative # Your MedianFinder object will be instantiated and called as such:
# mf = MedianFinder()
# mf.addNum(1)
# mf.findMedian()

边工作边刷题:70天一遍leetcode: day 86-1的更多相关文章

  1. 边工作边刷题:70天一遍leetcode: day 86

    Word Pattern II 要点: 注意与I的差异,其实题不难,看到这种迷乱的,首先要想到backtrack 1:1 mapping两个条件:p in and str in, or p not i ...

  2. 边工作边刷题:70天一遍leetcode: day 89

    Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...

  3. 边工作边刷题:70天一遍leetcode: day 77

    Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...

  4. 边工作边刷题:70天一遍leetcode: day 78

    Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...

  5. 边工作边刷题:70天一遍leetcode: day 85-3

    Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...

  6. 边工作边刷题:70天一遍leetcode: day 101

    dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...

  7. 边工作边刷题:70天一遍leetcode: day 1

    (今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...

  8. 边工作边刷题:70天一遍leetcode: day 70

    Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...

  9. 边工作边刷题:70天一遍leetcode: day 71-3

    Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...

  10. 边工作边刷题:70天一遍leetcode: day 71-2

    One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...

随机推荐

  1. 依赖于spring 4.x的spring组件

    1.Spring Data MongoDB 1.6.x开始依赖于spring 4.x: 2.@Conditional注解: 3.spring-data-redis 1.4.x开始依赖于spring 4 ...

  2. SharpGL学习笔记(十六) 多重纹理映射

    多重纹理就把多张贴图隔和在一起.比如下面示例中,一个表现砖墙的纹理,配合一个表现聚光灯效果的灰度图,就形成了砖墙被一个聚光灯照亮的效果,这便是所谓的光照贴图技术. 多重纹理只在OpenGL扩展库中才提 ...

  3. JavaScript Array(数组)对象

    一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...

  4. sqlserver允许远程连接的配置

    如果无法通过IP地址远程连接你服务器上的SQL Server服务器,可以参考下面的内容进行设置 在进行下述设置之前,应该确保你的网络已经安装设置完毕,服务器已经正常连接到网络中. 1.单击Window ...

  5. ArcGIS10.2下调试10.1的程序

    听说:10.2比10.1好,诚然,安装了10.2打开一看是这样的,以为是下载的版本问题,后来才以现是显示设置的问题. 因为,我使用的两个显示器,屏幕有点大,所以,就改成中等了,不然怎么可截取下面的截图 ...

  6. C#获取本地系统日期格式

    我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...

  7. IOS Quartz2D简介

    Quartz2D 简介( 后续会有相关应用) 第一部分 绘制直线 代码示例: - (void)drawRect:(CGRect)rect{ //获取图形上下文 CGContextRef cxConte ...

  8. 【原】ios下比较完美的单例模式,已验证

    网上关于ios单例模式实现的帖子已经很多了,有很多版本,里面有对的也有不对的.我在使用过程中很难找到一个比较完美的方法,索性自己写一个吧,经过项目验证是比较合理的一个版本. static PRAuto ...

  9. iOS中 @synthesize 和 @dynamic 区别

    OC object-c 为了让java的开发者习惯 使用.的操作,所以可以将接口类中的变量 使用@property来声明属性.但是在.h中声明的属性,必须在.m中使用@synthesize或者@dyn ...

  10. docker入门(1) Centos 7 下docker的安装

    centos 7安装docker 什么是 Docker Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go ...