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. oracle 根据字段分组取第一条数据及rank函数说明

    当前有这样一个需求,根据外键对子表数据进行分组,取每组中的一条数据就行了,如图: 如:COMMANDID = 26的有两条,只取一条数据. sql语句: select * from(select SY ...

  2. Eclipse中js文件修改后浏览器不能及时更新的解决办法

    项目中js文件修改后浏览器不能及时更新的解决办法 转载:http://www.codeweblog.com/%E9%A1%B9%E7%9B%AE%E4%B8%ADjs%E6%96%87%E4%BB%B ...

  3. Java NIO , AIO

    New IO: 特点:不再阻塞 Channel , Buffer Async IO: 特点:异步

  4. HeRaNO's NOIP CSP Round Day 2 T3 ginkgo

    睡醒后我第一眼:这不主席树裸题吗? 先统计dfs序,把树上问题转化为区间问题 区间大于等于某个数的个数...主席树模板? #include<bits/stdc++.h> #define r ...

  5. 人脸跟踪开源项目HyperFT代码算法解析及改进

    一.简介 人脸识别已经成为计算机视觉领域中最热门的应用之一,其中,人脸信息处理的第一个环节便是人脸检测和人脸跟踪.人脸检测是指在输入的图像中确定所有人脸的位置.大小和姿势的过程.人脸跟踪是指在图像序列 ...

  6. fatal: unable to connect to github.com npm install fail问题

    解决方法 git config --global url."https://".insteadOf git://

  7. sweetalert 弹框简单使用

    sweetalert网站 简单使用教程 拷贝文件 放到项目中 使用 页面效果 修改代码应用到事件中 成功删除演示(后台数据也会删除) 作 者:郭楷丰 出 处:https://www.cnblogs.c ...

  8. Android Scroller解析

    作用 这个类封装了滚动操作,如帮我们处理手指抬起来时候的滑动操作.与ViewGroup的scrollTo(),scrollBy()的生硬式移动,Scroller提供了一个更加柔和的移动效果.Scrol ...

  9. MYSQL入门这一篇就够了

    安装概述 分为5.6与,5.7版本,5.7的安装与5.6略有不同,因为依赖BOOST库,下面给出2个版本的安装脚本,直接运行即可 Mysql 5.6 [root@Tuiliu ~]# cat mysq ...

  10. JVM 理论基础目录(待更新,系列完全写完后会统一整理好)

    参考文档: [1] Java 虚拟机规范(Java SE 8版) [2] 深入理解 Java 虚拟机: JVM 高级特性与最佳实践 周志明 本系列的更新快慢全部随意,介意者请海涵. 一 .JVM 入门 ...