原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=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.

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.

Window position                Median
--------------- -----
[1 3 -1] -3 5 3 6 7 1
1 [3 -1 -3] 5 3 6 7 -1
1 3 [-1 -3 5] 3 6 7 -1
1 3 -1 [-3 5 3] 6 7 3
1 3 -1 -3 [5 3 6] 7 5
1 3 -1 -3 5 [3 6 7] 6

Therefore, return the median sliding window as [1,-1,-1,3,5,6].

Note: 
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.

题解:

使用minHeap和maxHeap来维护median. 为了方便,这里始终保持minHeap.size() == maxHeap.size() 或 minHeap.size() = maxHeap.size()+1.

所以最开始两个heap都为空时,加到minHeap中.

取median时若size相同就两边peek求和除以2. 若size不同,那么肯定minHeap size大, minHeap peek下就是median.

remove时,看要remove的数nums[i-k], 若比median小,从maxHeap中remove. 不然从minHeap中remove.

Note: 两遍peek求和时注意overflow.

Remove时如果出现小数, 多半会从小的这一侧remove, 也就是maxHeap中remove. 所以添加时应该尽量向大的这一侧添加. 但添加时检测要用!maxHeap.isEmpty()&&maxHeap.peek()>nums[i]限制小的一侧添加, 而不用minHeap.isEmpty() || minHeap.peek()<nums[i]胡乱往大的一侧添加. 因为有可能minHeap刚才经过remove已经空了, 若出现个很小的数就错误的加进了minHeap中.

Time Complexity: O(nk), n = nums.length. 对于minHeap 和 maxHeap来说每个元素add, remove O(1)次. remove(target) takes O(k).

Space: O(k).

AC java:

 public class Solution {
public double[] medianSlidingWindow(int[] nums, int k) {
if(nums == null || nums.length == 0 || k <= 0){
return new double[0];
} PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder()); int len = nums.length;
double [] res = new double[len-k+1];
for(int i = 0; i<=len; i++){
if(i>=k){
if(minHeap.size() == maxHeap.size()){
res[i-k] = ((double)minHeap.peek() + (double)maxHeap.peek())/2.0;
}else{
res[i-k] = minHeap.peek();
}
if(nums[i-k] < res[i-k]){
maxHeap.remove(nums[i-k]);
}else{
minHeap.remove(nums[i-k]);
}
}
if(i<len){
if(!maxHeap.isEmpty() && maxHeap.peek()>nums[i]){
maxHeap.offer(nums[i]);
}else{
minHeap.offer(nums[i]);
}
while(maxHeap.size() > minHeap.size()){
minHeap.offer(maxHeap.poll());
}
while(minHeap.size() - maxHeap.size() > 1){
maxHeap.offer(minHeap.poll());
}
}
}
return res;
}
}

类似Find Median from Data Stream.

LeetCode 480. Sliding Window Median的更多相关文章

  1. 480 Sliding Window Median 滑动窗口中位数

    详见:https://leetcode.com/problems/sliding-window-median/description/ C++: class Solution { public: ve ...

  2. 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...

  3. [LeetCode] Sliding Window Median 滑动窗口中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  4. Leetcode: Sliding Window Median

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  5. Sliding Window Median LT480

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  6. [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  7. LeetCode题解-----Sliding Window Maximum

    题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...

  8. [leetcode]239. Sliding Window Maximum滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  9. 滑动窗口的中位数 · Sliding Window Median

    [抄题]: 给定一个包含 n 个整数的数组,和一个大小为 k 的滑动窗口,从左到右在数组中滑动这个窗口,找到数组中每个窗口内的中位数.(如果数组个数是偶数,则在该窗口排序数字后,返回第 N/2 个数字 ...

随机推荐

  1. 每天一个Linux命令(44)crontab命令

        crontab命令被用来提交和管理用户需要周期性执行的任务,与windows下的计划任务类似.     (1)用法:     用法: crontab  [-u user]  file cron ...

  2. flex for循环

    //for ..in 循环中的迭代变量包含属性所保存的值和名称 //for each..in 循环中的迭代变量只包含属性所保存的值,而不包含属性的名称 //对象遍历,可以获取属性名称 private ...

  3. grep的若干用法

    查找包含server或者client的行 egrep 'server|client' file-name /usr/xpg4/bin/grep -E 'server|client' file-name ...

  4. 【HackerRank】 Find Digits

    Find Digits Problem Statement Given a number you have to print how many digits in that number exactl ...

  5. 基于linux的ekho(余音)安装与开发

    Ekho(余音)是一个把文字转换成声音的软件.它目前支持粤语.普通话(国语).诏安客语和韩语(试验中),英文则通过Festival间接实现.它比eSpeak的设计更简易,但文件较大.由于使用了真人发声 ...

  6. 【转载】格式化存储装置成为 Ext2/Ext3/Ext4 档案系统

    格式化 用系统管理员帐户 (即 root) 身份打「mkfs -t ext2|ext3|ext4 储存装置」: mkfs -t ext3 /dev/sdb5 要格式化档案系统为 Ext2,亦可以直接使 ...

  7. Django---Blog系统开发之建库

    数据库配置: #sqlite3数据库配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os. ...

  8. 转移灶,原发灶,cfDNA的外显子测序得到的突变点的关系

    文章名称:Exome Sequencing of Cell-Free DNA from Metastatic Cancer Patients IdentifiesClinically Actionab ...

  9. How to Google

    程序员的基础生存技能 -- 关于搜索引擎的小贴士 如果票选近二十年最伟大的发明,我相信搜索引擎肯定会占据一个不容小觑的位置,它不单是一项发明,更是一项成就,最大程度消灭了信息的不平等.既然人人都可以接 ...

  10. java基础10(IO流)-字节流

    IO流 输入与输出[参照物是程序] 如果从键盘.文件.网络甚至是另一个进程(程序或系统)将数据读入到程序或系统中,称为输入 如果是将程序或系统中的数据写到屏幕.硬件上的文件.网络上的另一端或者是一个进 ...