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. PowerBuilder学习笔记之1开发环境

    Powerbuilder Classic 12.5开发环境(PB经典 12.5) 教材链接:https://wenku.baidu.com/view/5e087d6ab9f67c1cfad6195f3 ...

  2. python selenium IE Firxfor pyinstaller

    以前在python环境下selenium 主要用的是chromdriver,这次发现老是报错(Timeout), 实际又是正确的, 可能是和chrome版本不正确,再加上我程序蹦来就在windows环 ...

  3. APIO2019简要题解

    Luogu P5444 [APIO2019]奇怪装置 看到这种题,我们肯定会想到\((x,y)\)一定有循环 我们要找到循环节的长度 推一下发现\(x\)的循环节长为\(\frac{AB}{B+1}\ ...

  4. MySQL面试题及答案整理,史上最全!

    原文链接:https://juejin.im/post/5d351303f265da1bd30596f9 前言 本文主要受众为开发人员,所以不涉及到MySQL的服务部署等操作,且内容较多,大家准备好耐 ...

  5. c# winform结合数据库动态生成treeview的父节点和子节点方法和思路

    tb_food表的结构如图一: tb_foodtype表的结构如图二: //获取tb_foodtype表中的所有数据 private void InitDataTable() { SqlConnect ...

  6. Windows 查看端口占用进程并关闭

    当我们在运行一些软件需要特定软件(如tomcat)时,有可能会碰上端口被占用的情况,这时候我们可能就需要更改端口或把占用端口的进程结束掉,因为更换端口可能会导致当前环境产生一些的问题或是需要重新配置其 ...

  7. 2019 东方财富网java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.东方财富网等公司offer,岗位是Java后端开发,因为发展原因最终选择去了东方财富网,入职一年时间了,也成为 ...

  8. 2019 花椒直播java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.花椒直播等公司offer,岗位是Java后端开发,因为发展原因最终选择去了花椒直播,入职一年时间了,也成为了面 ...

  9. Java自学-接口与继承 隐藏

    Java中的方法隐藏 与重写类似,方法的重写是子类覆盖父类的对象方法 隐藏,就是子类覆盖父类的类方法 步骤 1 : 父类 父类有一个类方法 :battleWin package charactor; ...

  10. 编写可维护的JavaScript-随笔(三)

    UI层的松耦合 本章提出了一个概念就是耦合 假设修改一个组件的时候需要修改很多其他的组件的话则表示组件之间存在紧耦合 如果修改一个组件而不需要修改其他组件的时候就做到了松耦合 页面是由HTML.CSS ...