"""
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. List(数组)里面常用的属性和方法

    常用属性: length 长度 reversed 翻转 isEmpty 是否为空 isNotEmpty 是否不为空常用方法: add 增加 addAll 拼接数组 增加多个数据 list.addAll ...

  2. 《JavaScript高级程序设计》读书笔记(五)引用类型

    内容---使用对象---创建并操作数组---理解基本的JavaScript类型---使用基本类型和基本包装类型 引用类型--引用类型的值(对象)是引用类型的一个实例--在ECMAScript中,引用类 ...

  3. 我的 Python 编码规范

    python 文件的组成 为了便于描述,先上一个 demo #!/usr/bin/env python # -*- coding: utf-8 -*- """通常这里是关 ...

  4. 无数据库模式kong/kong-ingress-controller

    apiVersion: v1kind: Namespacemetadata:  name: kong---apiVersion: apiextensions.k8s.io/v1beta1kind: C ...

  5. linux的端口学习(一)

    1.端口是什么? 1.1 是英文port的意译,可认为是设备与外界通讯交流的出口. 1.2 端口可分为虚拟端口和物理端口. 1.2.1 虚拟端口:指计算机内部或交换机路由器内的端口,不可见.例如计算机 ...

  6. Java 去除字符串前后指定的字符

    一.去除字符串中的中文字符. /** * 去除字符串中的中文字符 * * 示例:brandName值为: 中国ABCD88深圳 * * 返回: ABCD88 * * @param brandName ...

  7. Cisco AP-ROMMON升级AP镜像

    Rommon is Cisco bootloader for their Router devices >>>ROMMON是思科设备的引导加载程序while U-boot is a ...

  8. 【Hutool】工具类之日期时间工具-DateUtil

    日期时间工具类 一.依赖 <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-al ...

  9. 十八、sun JPA理解及使用

    1.JPA理解及实现:    JPA(Java Persistence API)作为Java EE 5.0平台标准的ORM规范,将得到所有Java EE服务器的支持,是SUN在充分吸收现有ORM框架的 ...

  10. iOS 开发之 设计模式【一】原型模式 (Prototype pattern)

    原型模式(Prototype pattern): 定义:使用原型实例指定创建对象的种类,并通过复制这个原型创建对象.也可以理解为模板,在创建新对象的时候,按照模板的方法来复制,避免重复造轮子. 简单来 ...