Description

Given an array of n integer, and a moving window(size k), move the window at each iteration from the start of the array, find the median of the element inside the window at each moving. (If there are even numbers in the array, return the N/2-th number after sorting the element in the window. )

Example

Example 1:

Input:
[1,2,7,8,5]
3
Output:
[2,7,7] Explanation:
At first the window is at the start of the array like this `[ | 1,2,7 | ,8,5]` , return the median `2`;
then the window move one step forward.`[1, | 2,7,8 | ,5]`, return the median `7`;
then the window move one step forward again.`[1,2, | 7,8,5 | ]`, return the median `7`;

Example 2:

Input:
[1,2,3,4,5,6,7]
4
Output:
[2,3,4,5] Explanation:
At first the window is at the start of the array like this `[ | 1,2,3,4, | 5,6,7]` , return the median `2`;
then the window move one step forward.`[1,| 2,3,4,5 | 6,7]`, return the median `3`;
then the window move one step forward again.`[1,2, | 3,4,5,6 | 7 ]`, return the median `4`;
then the window move one step forward again.`[1,2,3,| 4,5,6,7 ]`, return the median `5`;

Challenge

O(nlog(n)) time

思路:使用两个PriorityQueue, 依次遍历元素,当元素小于最大堆堆顶或最大堆为空则放入最大堆,否则放入最小堆。同时 保证maxHeap的size比minHeap多一个或相等,median即为最大堆的堆叠元素。

public class Solution {
/**
* @param nums: A list of integers
* @param k: An integer
* @return: The median of the element inside the window at each moving
*/ private PriorityQueue<Integer> maxHeap, minHeap;
public List<Integer> medianSlidingWindow(int[] nums, int k) {
List<Integer> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
int n = nums.length;
maxHeap = new PriorityQueue<Integer>(n, Collections.reverseOrder());
minHeap = new PriorityQueue<Integer>(n);
for (int i = 0; i < n; i++) {
if (i - k >= 0) {
if (nums[i - k] > maxHeap.peek()) {
minHeap.remove(nums[i - k]);
} else {
maxHeap.remove(nums[i - k]);
}
balance();
}
if (maxHeap.size() == 0 || nums[i] < maxHeap.peek()) {
maxHeap.offer(nums[i]);
} else {
minHeap.offer(nums[i]);
}
balance(); if (i - k >= -1) {
res.add(maxHeap.peek());
}
}
return res;
} private void balance() {
// 保证maxHeap的size比minHeap多一个或相等 while (maxHeap.size() < minHeap.size()) {
maxHeap.offer(minHeap.poll());
} while (minHeap.size() < maxHeap.size() - 1) {
minHeap.offer(maxHeap.poll());
}
}
}

  

Sliding Window Median的更多相关文章

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

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

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

  4. LeetCode 480. Sliding Window Median

    原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description 题目: Median is the middl ...

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

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

  6. LintCode "Sliding Window Median" & "Data Stream Median"

    Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...

  7. Lintcode360 Sliding Window Median solution 题解

    [题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...

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

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

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

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

随机推荐

  1. 在C++中调用FFTW

    FFTW是一个可以进行可变长度一维或多维DFT的开源C程序库,是目前最快的FFT算法实现. 本文简述了在Windows平台上,如何在C++中调用FFTW,所使用的IDE为Visual Studio 2 ...

  2. Elastic Stack 7.5.0白金版永不过期

    适用版本:7.4.0~7.5.0 警告:本文章仅限于学习,非商业用途. 目录结构 # 先创建相关目录,具体结构如下: /opt |-- bulid # 编译目录 | |- src |-- instal ...

  3. JAVA中List,Map,Set接口的区别

    从三点来分析它们之间的不同: 1.继承的接口不同: List,Set接口都是继承于Collection接口的,而Map接口不是,它是一个顶层接口. 2.自身特点: List:用来处理序列的.对于放于的 ...

  4. linux安装php nginx mysql

    linux装软件方式: systemctl status firewalld.service 查看防火墙systemctl stop firewalld.service systemctl disab ...

  5. 【转载】如何删除Windows远程桌面保存的账号密码数据

    在Windows系统中,无论是win7.win8还是win10系统,都可使用Windows系统自带的远程桌面连接工具来远程服务器,很多时候Windows远程桌面在连接一次后会自动保存连接的账号密码等信 ...

  6. 2.JavaScript中的原型规则以及原型设计模式

    原型规则 原型规则 所有的引用类型(数组.对象.函数),都具有对象特征,即可自由扩展属性: 所有的引用类型,都有一个_proto_ 属性(隐式原型),属性值是一个普通对象: 所有函数,都具有一个pro ...

  7. JavaScript 之 定时器

    JavaScript 里面有两个定时器:setTimeout() 和 setInterval() . 区别: setTimeout():相当于一个定时炸弹,隔一段时间执行,并且只会执行一次就不在执行了 ...

  8. gulp与webpack的区别?是一种工具吗?

    问:gulp和webpack什么关系,是一种东西吗?可以只用gulp,不用webpack吗 或者反过来?有什么区别? 答:gulp是工具链.自动化构建工具,可以配合各种插件,我们不用再做机械重复的工作 ...

  9. JAVA - Windows下JDK自动设置脚本

    CMD窗口如果使用下面脚本代码出现乱码,则按照下文处理后,就可以了: https://www.cnblogs.com/sunylat/p/11308037.html @echo off echo ** ...

  10. 搞不懂JS中赋值·浅拷贝·深拷贝的请看这里

    前言 百科定义:拷贝就是拷贝指向对象的指针,意思就是说:拷贝出来的目标对象的指针和源对象的指针指向的内存空间是同一块空间,浅拷贝只是一种简单的拷贝,让几个对象公用一个内存,然而当内存销毁的时候,指向这 ...