当前位置:网站首页>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。
边栏推荐
猜你喜欢
HCIP第十三天
E-commerce data warehouse ODS layer-----log data loading
From September 1st, my country has granted zero-tariff treatment to 98% of tax items from 16 countries including Togo
一体化HTAP数据库如此难,为什么他们还要做?
Nacos配置文件管理、微服务获取Nacos配置文件
XSS online shooting range---haozi
Markdown syntax
IO线程进程->线程同步互斥机制->day6
主板设计中:网络变压器与RJ45网口之间应该保持什么样的距离?
idea2021配置svn报错Cannot run program “svn“ (in directory “xxx“):CreateProcess error=2,系统找不到指定的文件
随机推荐
安全基础8 ---XSS
2021年数据泄露成本报告解读
函数,递归以及dom简单操作
开源一夏 |如何优化线上服务器
ValidationError: Progress Plugin Invalid Options
编译器工程师眼中的好代码(1):Loop Interchange
ES、Kibana 8.0安装
东西向和南北向通信的统一
C. Keshi Is Throwing a Party- Codeforces Global Round 17
Several difficult problems in DDD
XSS testing
模板字符串
LyScript 实现应用层钩子扫描器
云图说丨初识华为云微服务引擎CSE
HCIP第十六天
How to deal with commas in the content of the CSV file of the system operation and maintenance series
Android build error: Plugin with id 'kotlin-android' not found.
D - Project Planning--二分
StoneDB 助力 2022 开放原子全球开源峰会
nxp官方uboot移植到野火开发板PRO(修改LCD部分和网络部分)