边工作边刷题:70天一遍leetcode: day 86-1
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
- 注意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的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 86
Word Pattern II 要点: 注意与I的差异,其实题不难,看到这种迷乱的,首先要想到backtrack 1:1 mapping两个条件:p in and str in, or p not i ...
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
随机推荐
- Windows下 C++ 实现匿名管道的读写操作
由于刚弄C++没多久,部分还不熟练,最近又由于开发需求要求实现与其他程序进行通信,瞬间就感觉想到了匿名通信.于是自己查阅了一下资料,实现了一个可读可写的匿名管道: 源代码大部分都有注释: Pipe.h ...
- No.001:Two Sum
问题: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- CentOS7 Debian 8 安装VMware-tools
如在安装过程中碰到未找到gcc 或者 kernel headers的可按以下方案解决,适用任意版本 CentOS 7 1. Update the kernel: $ yum update kernel ...
- ArrayList,Hashtable,List<T>,Dictionary<K,V>
1.ArrayList ArrayList list = new ArrayList(); //for遍历 ; i < list.Count; i++) { SE se=(SE)list[i]; ...
- 设计模式总结篇系列:观察者模式(Observer)
观察者模式中通常有两个基本的概念主题:观察者和被观察者.当被观察者状态发生改变时,需要通知相应的观察者,当然,每个被观察者所对应的观察者可能不知一个,他们之间是1:n的关系.用专业一点的术语对观察者模 ...
- C# 线程同步
Mutex 类 使用Mutex类来同步两个单独的程序.Mutex是一种原始的同步方式,其只对一个线程授予对共享资源的独占访问. const string NutexName = "C&quo ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q121-Q124)
Question 121You develop a custom approval workflow. The workflow uses the CreateTask class to assign ...
- android notification 传值关键
android notification 传值关键在 onNewIntent方法里获取 @Override protected void onCreate(Bundle savedInstanceSt ...
- swift GCD使用指南
swift GCD使用指南 Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适 ...
- Python基础(3)--列表和元组
Python包含6种内建序列:列表.元组.字符串.Unicode字符串.buffer对象.xrange对象 本篇主要讨论最常用的两种类型:列表.元组 本文地址:http://www.cnblogs.c ...