当前位置:网站首页>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。
边栏推荐
- 主板设计中:网络变压器与RJ45网口之间应该保持什么样的距离?
- 电商数仓ODS层-----日志数据装载
- Android build error: Plugin with id 'kotlin-android' not found.
- 基于DMS的数仓智能运维服务,知多少?
- ValidationError: Progress Plugin Invalid Options
- XSS practice - cycle and two cycle problem at a time
- 一体化HTAP数据库如此难,为什么他们还要做?
- CAS:1620523-64-9_Azide-SS-biotin_biotin-disulfide-azide
- 《富爸爸,穷爸爸》思维导图和学习笔记
- XSS漏洞复现
猜你喜欢
nxp官方uboot移植到野火开发板PRO(无任何代码逻辑的修改)
[kali-vulnerability scanning] (2.1) Nessus download and installation (on)
关于GPIO你真的懂了吗?这篇文章都给你整理好了
HCIP第十五天
2022-8-3 第七组 潘堂智 锁、多线程
距LiveVideoStackCon 2022 上海站开幕还有3天!
反射机制
E - Swap
Zero trust, which has been popular for more than ten years, why can't it be implemented?
Five Steps to Detect and Control Shadow IT
随机推荐
IO thread process -> thread synchronization mutual exclusion mechanism -> day6
【使用 Pytorch 实现入门级的人工神经网络】
tidyverse based on data.table?
LyScript 实现应用层钩子扫描器
C. Divan and bitwise operations
STP生成树
Soft exam system analysts note experience sharing: theory of protracted war
FVCOM三维水动力、水交换、溢油物质扩散及输运数值模拟丨FVCOM模型流域、海洋水环境数值模拟方法
AI首席架构师13-AICA-智能文档分析技术在行业场景中的应用
385. Mini Parser
dataframe multi-level index replace index df.swaplevel(axis=1)
win10安装及配置Gradle
CAS:122567-66-2_DSPE-Biotin_DSPE-Biotin
码率vs.分辨率,哪一个更重要?
聚焦开源与联合共创|麒麟软件出席开源峰会欧拉分论坛
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:1260586-88-6_Biotin-C5-Azide_Biotin-C5-Azide
template string
[kali-vulnerability scanning] (2.1) Nessus download and installation (on)
376. Wiggle Subsequence