[LeetCode] 295. Find Median from Data Stream @ python

leetcode295 一道题认识大顶堆和小顶堆 LC295:数据流的中位数简单来说就是计算中位数。这里采用大顶堆和小顶堆来分别存储中位数的两边。堆排序(Heap Sort)是利用堆这种数据结构所设计的一种排序算法。堆排序先按从上到下、从左到右的顺序将待排序列表中的元素构造成一棵完全二叉树,然后对完全二叉树进行调整,使其满足:构建出堆后,将堆顶与堆尾进行交换,然后将堆尾从堆中取出来,取出来的数据就是最大(或最小)的数据。重复构建堆并将堆顶和堆尾进行交换,取出堆尾的数据,直到堆中的数据全部被取出,列表排序完成。堆结构分为大顶堆和小顶堆:大顶堆:每个节点(叶节 阅读详情

一.题目:
寻找数据流中的中位数,即数据会不断更新,求这个动态过程的中位数.
二.解题思路:
利用最大堆和最小堆实现,控制两个堆的数量相等.
代码如下:

class MedianFinder(object):

    def __init__(self):
        self.max_heap = [] # for half left
        self.min_heap = [] # for half right

    def addNum(self, num):
        size_left = len(self.max_heap)
        size_right = len(self.min_heap)
        #如果两个堆都是空的,那么把数字放到最小堆中
        if size_left == 0 and size_right == 0:
            heapq.heappush(self.min_heap, num)
            return
        
        if self.max_heap:
            left_val = -self.max_heap[0]
        if self.min_heap:
            right_val = self.min_heap[0]
    
        #如果两个堆数量相等,那么先考虑把数字放入到最大堆.然后在将排出的堆顶放入到最小堆中
        
        if size_left == size_right:
            #如果数字比最大堆中的最大数还大,说明最大堆中排出的堆顶肯定还是它,所以直接把它放入到最小堆中
            if num < left_val:
                #由于使用的是heapq库,只适用于最小堆,所以我们需要把数字取反
                heapq.heappush(self.max_heap, -num) 
                tmp = heapq.heappop(self.max_heap)
                heapq.heappush(self.min_heap, -tmp)
            else:
                heapq.heappush(self.min_heap, num)
                
        if size_left < size_right:
            if num < right_val:
                heapq.heappush(self.max_heap, -num)
            else:
                tmp = heapq.heappop(self.min_heap)
                heapq.heappush(self.max_heap, -tmp)
                heapq.heappush(self.min_heap, num) 

    def findMedian(self):
        size_left = len(self.max_heap)
        size_right = len(self.min_heap)
    
        if size_left == size_right:
            return (-self.max_heap[0] + self.min_heap[0])/2.0
        elif size_left > size_right:
            return float(-self.max_heap[0])
        else:
            return float(self.min_heap[0])


# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()
Unity 数字孪生笔记1 工具介绍 前言 工欲善其事必先利其器 流程思考 数字孪生本质上一种基于实际物理数据的可视化方案,通过对接实际数据,在三维界面中展示孪生结果。 那么这个流程就很简单了。 整个流程分为三块:数据来源-》数据分析-》数据展示 输入数据 首先我们需要考虑我们能拿到什么。第一,实际物体的三维数据从哪里来,第二,实际数据怎么获取到。 模型数据 工业上常常会用到CAD模型,CAD模型具有精确、统一的特点,但是CAD不是三角面片,我们不能直接导出到Unity中,而且导入之后有很多不合理的地方,例如面片杂乱、面数过高等问题。针对这个 阅读详情

相关推荐

有限元缺陷:剪切锁死

有限元缺陷:剪切锁死

wsxww11的博客 5190

LeetCode-295 数据流的中位数

# 中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。 # # 例如, # [2,3,4] 的中位数是 3 # [2,3] 的中位数是 (2 + 3) / 2 = 2.5 # # 设计一个支持以下两种操作的数据结构: # void addNum(int num) - 从数据流中添加一个整数到数据结构中。 # double findMedian() - 返回目前所有元素的...

今晚打老虎 417

Verilog实现FIR滤波器

1、FIR滤波器简介 不追究FIR滤波器的深层含义,我们只关注如何实现;可以看出,FIR滤波器的本质就是延迟、系数相乘与求和,如下图: 2、设计 基于以上分析,按照三级流水实现FIR滤波器设计:信号延迟-系数相乘-求和 `timescale 1ns / 1ps module fir( input clk, input rst_n, input [3:0]din, output reg [9:0]dout ); // data reg reg [3:0]din1; reg [3:.

CLL_caicai的博客 1万+

LeetCode 295题:数据流的中位数(Python中堆的使用)

题目介绍 295. Find Median from Data Stream 中位数是一个有序数组中中间数的值。如果数组的长度为偶数,中位数就是中间两个数的均值。 解法 这道题的解法有多种,但是推荐的解法之一就是使用大小堆进行实现。思维过程如下: 如果能够直接得到中位数,那么寻找中位数的操作的时间复杂度为O(1) 如果能够找到一种合理的插入数据的方法,则数据增加带来的复杂度就会被控制到尽可能的小...

持久决心的博客 1563

STM32移植LVGL(LittleVGL)

**STM32移植LVGL(LittleVGL)**

qq_53000374的博客 1万+

基于GPU的GEMM矩阵相乘运算优化

从上图中我们可以看到三种处理方法。第一种是将A和B矩阵分块,第二种方法是将C和B矩阵分块,第三种方法是将C和A矩阵分块。GEMM的子任务是GEPP或GEMP;最小粒度的任务是GEBP或GEPB或点乘。

weixin_55035144的博客 2156

LeetCode-Python-295. 数据流的中位数

中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。 例如, [2,3,4]的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2.5 设计一个支持以下两种操作的数据结构: void addNum(int num) - 从数据流中添加一个整数到数据结构中。 double findMedian() - 返回目前所有元素的中位数。 示例: ad...

Keep Coding 1792

STM32L051低功耗模式下LPUART的使用

STM32L0系列低功耗UART配置代码清单及注释,大家可参考学习使用。

嵌入式工程师 2104

Python堆排序,以及堆的应用(LeetCode-295、215)

python堆排序 堆排思路 建立大根堆:将数组中所有数入堆,即对所有数进行一遍 heap_insert() 操作; 将该大根堆堆顶元素与堆中最后一个元素交换,然后 heap_size - 1 ; 对当前堆顶变化的元素,进行一次 heapify() 操作,使heap重新变为大根堆; 重复2~3步骤,直至 heap_size = 1 . 堆排序代码 实际上主要是写清楚heap_insert()和heapify()的操作 class Solution(): def heap_sort(self, n

谁谓荼苦,其甘如荠 629

软考 系统架构设计师历年真题集萃(248)

软考 系统架构设计师历年真题集萃(248)

phmatthaus的专栏 792

【FPGA教程案例49】控制案例1——基于FPGA的PID控制器verilog实现

PID控制器产生于1915年,PID控制律的概念最早是由LYAPIMOV提出的,到目前为止,PID控制器以及改进的PID控制器在工业控制领域里最为常见。PID控制器(比例-积分-微分控制器),由比例单元 P、积分单元 I 和微分单元 D 组成。通过Kp,Ki和Kd三个参数的设定。PID控制器主要适用于基本线性和动态特性不随时间变化的系统。图1为PID控制器的基本结构框图。PID控制器以其结构简单、稳定性好、工作可靠、调整方便而成为工业控制的主要技术之一。...

FPGA/MATLAB学习教程/源码/项目合作开发 4531

Leetcode 295. Find Median from Data Stream (python+cpp)

Leetcode 295. Find Median from Data Stream题目解法1:simple sorting解法2:插入解法3:two heaps 题目 解法1:simple sorting 每次找中位数的操作都将列表进行重新排序,这种解法时间复杂度为O(nlogn) class MedianFinder: def __init__(self): """ initialize your data structure here. ""

qq_37821701的博客 692

Swin Transformer 与 CNN 结合实现图像分类

Swin Transformer 与 CNN 结合实现图像分类。(含swin transformer 结构讲解,cnn网络搭建,代码)

这里是Mae。 5027

[leetcode] 295. Find Median from Data Stream @ python

原题 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...

闲庭信步 933

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

[leetcode] 295. Find Median from Data Stream 解题报告

题目链接: https://leetcode.com/problems/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 middle value. So the median i

小榕流光的专栏 3591

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

【LeetCode】295. Find Median from Data Stream 解题报告(C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法大根堆+小根堆日期 题目地址:https://leetcode.com/problems/find-median-from-data-stream/ 题目描述 Median is the middle value in an ordered integer ...

负雪明烛 1527

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[Leetcode]295. Find Median from Data Stream

Leetcode 295. Find Median from Data Stream

正西风落叶下长安 956

LeetCode #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 no middle value. So the median is the mean of the two middle value. For example, [2,3,4], the ...

LawFile的博客 243

[LeetCode]295. Find Median from Data Stream

题意:在可变数组下,高效地寻找这一组数的中位数。 思路: 【TLE】①使用插入排序。时间复杂度:O(logn) + O(n) ~ O(n)。 【TLE】②使用快速排序。时间复杂度:O(nlogn) 【AC】③使用双堆排序。时间复杂度:O(5*logn)。

哈哈哈哈嘿嘿嘿 391

[Leetcode] 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 no middle value. So the median is the mean of the two middle value. Examples:  [2,3,4] , t

魔豆(Magicbean)的博客 805

[LeetCode] 295. Find Median from Data Stream_hard tag: Heap Need to update the follow up question?

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 ...

weixin_30782331的博客 172

[Leetcode] #295 Find Median from Data Stream (Heap)

DiscriptionMedian 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 m...

奔跑的小乌龟 391

数据流中的中位数python_LeetCode #295 Find Median from Data Stream 数据流的中位数

Description: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...

weixin_42298645的博客 268

[LeetCode]--295. Find Median from Data Stream(Max-Heap & Min-Heap)

[LeetCode]--295. Find Median from Data Stream Max-Heap & Min-Heap Java_PriorityQueue

beijingbuaaer的博客 302

面试大厂leetcode重点题型简洁明快复习(dfs/bfs,动态规划,链表,滑动窗口/双指针,回溯,ACM型输入输出,二分)

笔者正在应聘大厂的春招岗位,笔者筛选了一下经常遇到的面试题型重点dfs/bfs动态规划链表滑动窗口/双指针回溯ACM型输入输出二分),笔者这里也尽量挑里面的最重点题型,从问题入手,对这些题型进行简洁明快的复习,其他的一些数据结构的题,也考,但笔者认为不是重点,这里就不提了。

weixin_44151034的博客 586

双堆法实现数据流中位数的高效计算

中位数作为描述数据分布的关键统计量,在实时数据处理中面临独特挑战。传统基于排序的方法时间复杂度达O(nlogn),难以满足高频数据流处理需求。双堆算法通过维护一个最大堆和最小堆,将插入复杂度降至O(logn)同时保持O(1)查询能力。这种数据结构组合利用了堆的极值特性:最大堆保存较小半数,最小堆保存较大半数,通过平衡堆大小确保中位数始终位于堆顶。该技术在金融实时分析、物联网传感器处理等场景表现优异,也是LeetCode热题中的经典案例。算法实现需注意整数溢出、并发访问等边界条件,可通过JMH基准测试验证其微

weixin_34050519的博客 477
上一篇: [LeetCode] 23. Merge k Sorted Lists @ python
下一篇: [LeetCode] 451. Sort Characters By Frequency @ python
tmfighting
博客等级 码龄9年 3粉丝 67原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值