当前位置:网站首页>480. Sliding Window Median
480. Sliding Window Median
2022-08-03 21:45:00 【51CTO】
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
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Your job is to output the median array for each window in the original array.
For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.
Therefore, return the median sliding window as [1,-1,-1,3,5,6].
Note:
You may assume k is always valid, ie: k is always smaller than input array’s size for non-empty array.
思路:
这题和那道Find Median from Data Stream比起来多加了个sliding window。那道题巧妙的用了两个heap来找到mean,还有道题是Slide Window Maximum,同样是slide window的题。还是用两个heap来做,remove这个操作复杂度用了logk。minHeap和maxHeap,maxHeap在保存较小的一半元素,minHeap保存较大的一半元素,0 <= minHeap.size() - maxHeap.size() <= 1,注意maxheap写的时候不能用a - b,因为可能overflow。
边栏推荐
- Security Fundamentals 8 --- XSS
- 手动输入班级人数及成绩求总成绩和平均成绩?
- CAS:122567-66-2_DSPE-Biotin_DSPE-Biotin
- CAS:908007-17-0_Biotin-azide _生物素叠氮化物
- CAS:1192802-98-4_UV 裂解的生物素-PEG2-叠氮
- nxp官方uboot移植到野火开发板PRO(修改LCD部分和网络部分)
- LVS负载均衡集群
- D - Project Planning--二分
- FVCOM三维水动力、水交换、溢油物质扩散及输运数值模拟丨FVCOM模型流域、海洋水环境数值模拟方法
- 376. Wiggle Subsequence
猜你喜欢
随机推荐
XSS线上靶场---prompt
CAS: 773888-45-2_BIOTIN ALKYNE_生物素-炔基
CAS:1620523-64-9_Azide-SS-biotin_biotin-disulfide-azide
Markdown syntax
反射机制
StoneDB 开源社区月刊 | 202207期
易基因:植物宏病毒组研究:植物病毒的进化与生态 | 顶刊综述
深度学习和机器学习有什么区别?
FVCOM 3D Numerical Simulation of Hydrodynamics, Water Exchange, Dispersion and Transport of Oil Spills丨FVCOM Model Watershed, Numerical Simulation Method of Marine Water Environment
CAS:908007-17-0_Biotin-azide_Biotin azide
What is the role and difference between buildscript and allprojects?
STP生成树
函数,递归以及dom简单操作
软件测试人员必备的60个测试工具清单,建议收藏一波~
一体化HTAP数据库如此难,为什么他们还要做?
LitJson报错记录
数据一致性:双删为什么要延时?
376. Wiggle Subsequence
XSS online shooting range---Warmups
From September 1st, my country has granted zero-tariff treatment to 98% of tax items from 16 countries including Togo









