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. IE Unknown runtime error

    1. 在函数中使用原生的js的时候,有时在IE下会出现Unknown runtime error 火狐下正常 2. 解决办法, 将原生js改成jquery处理兼容问题 document.getElem ...

  2. SQL Server 全局变量

    SQL Server中所有全局变量都使用两个@符号作为前缀 --1.@@error 最后一个T-SQL错误的错误号(目的是或得违反约束的错误号) insert into Subject values( ...

  3. css3实现switch开关效果

    之前阿里电面的时候问的一个问题,今天抽时间做了个demo. html结构 <div class="container"> <div class="bg_ ...

  4. Angularjs中对时间格式:/Date(1448864369815)/ 的处理

    注:本文使用的 angular 版本为 1.3 版 我们在后台对数据进行json序列化时,如果数据中包含有日期,序列化后返回到前端的结果可能是这样的: /Date(1448864369815)/  . ...

  5. rails provide与content_for的区别

    页面渲染时:provide先执行,但找到一个provide之后就不再查找 content_for 顺序执行,在哪个位置,就等之前的渲染完后才执行.但是要等到所有的content被查找完后一块返回,也就 ...

  6. javascript-this,call,apply,bind简述1

    最近在系统的学习面向对象方面的知识,遇到的最大拦路虎就数this的指向,call,apply,bind函数的使用,单独抽出一天时间把这几个烦人的家伙搞定,去学习更深入的内容. 首先介绍一下this的一 ...

  7. MSCRM 2015 新功能(一)

    MS官网信息:http://www.microsoft.com/en-us/dynamics/crm-customer-center/what-s-new.aspx MS官网中提到的新功能主要以下几块 ...

  8. SharePoint 2013 术语和术语集介绍

    托管元数据是一个集中管理的术语的分层集合,我们可以定义术语和术语集,然后将其用作 SharePoint Server 2013 中项目的属性.简单的说,术语是一个可与 SharePoint Serve ...

  9. 2015年第7本(英文第6本):纳尼亚传奇I–狮子、女巫、魔衣橱

    书名: The Chronicles of Narnia 1 — The Lion, the Witch and the Wardrobe 作者:C.S. Lewis 单词数:4.2万 不重复单词数: ...

  10. 在XcodeGhost事件之后,获取更纯净的Xcode的方法。

    正值Xcode 7正式版本的更新,IOS界就冒出了个甚至可以说成涉及国家安全的大事也不为过的事件: 也可以点击网址链接看总结的更完整的文章:众多知名 APP 都中毒了,XCodeGhost 病毒事件汇 ...