【题目描述】

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

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

【题目链接】

www.lintcode.com/en/problem/sliding-window-median/

【题目解析】

这道题和Data Stream Median类似,也是寻找动态数组的media,因此也可以用maxheap+minheap来解。随着窗口的移动,每次先加入一个新元素,再删去一个旧元素,再使两个heap中元素数量平衡即可。

用一个最大堆来记录较小一半的元素,一个最小堆来记录较大一半的元素。

先初始化最初的窗口里的k个元素。将元素加入最大堆,若为奇数次,则比较最大堆堆顶和最小堆堆顶元素大小,若最大堆堆顶元素大于和最小堆堆顶元素,则交换两个堆顶元素,若为偶数次,则将最大堆堆顶元素加入最小堆。

然后开始移动窗口,先将新元素加入,若比原median小,则加入最大堆,反之则加入最小堆。然后删除窗口最前面的一个元素。

然后调整最大堆和最小堆的大小。根据k值可以分两种情况讨论:

1)若k为偶数,则需要最大堆中元素和最小堆中元素数量相等

2)若k为奇数,则需要最大堆中元素比最小堆中元素多1个

此时最大堆的堆顶元素即为此时窗口元素的median

【参考答案】

www.jiuzhang.com/solutions/sliding-window-median/

Lintcode360 Sliding Window Median solution 题解的更多相关文章

  1. LeetCode 480. Sliding Window Median

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

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

  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. 滑动窗口的中位数 · Sliding Window Median

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

  8. Sliding Window Median

    Description Given an array of n integer, and a moving window(size k), move the window at each iterat ...

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

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

随机推荐

  1. MySQL如何正确查询字段为NULL的字段

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  2. Netbeans简要配置许可证信息

    <#if licenseFirst??>${licenseFirst}</#if>${licensePrefix}Copyright (C) <2017>  < ...

  3. 关于FFMPeg-PHP你必须要知道的

    #PHP FFmpeg [![Build Status](https://secure.travis-ci.org/PHP-FFMpeg/PHP-FFMpeg.png?branch=master)]( ...

  4. Luogu P1078 文化之旅

    题目描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不同的国家可能有相同的文化.不同文化 ...

  5. SpringMVC的数据转换,格式化和数据校验

          在SpringMVC中,根据请求方法签名不同,将请求消息中的消息以一定的方式转换并绑定到请求方法的参数中,在请求信息到达真正调用处理方法的这一段时间内,SpringMVC还会完成很多其他的 ...

  6. linux pxe网络装机无人值守

    项目分析远程装机的实现:配置DHCP+HTTP+TFTP提供通过vesamenu.c32模块实现图形PXE菜单为不同系统分别提供ks应答文件将第三方rpm包以yum源的方式提供:集中提供ntfs-3g ...

  7. 【html5】html5本地简单存储

    html5本地简单存储 HTML5 提供了四种在客户端存储数据的新方法,即 localStorage .sessionStorage.globalStorage.Web Sql Database. 前 ...

  8. Python Numpy包安装

    1,下载python 下载地址: https://www.python.org/downloads/windows/ 2,配置python环境变量 在电脑的系统属性的系统变量path中添加python ...

  9. SQL Server 2008 R2 添加登录账户配置权限

    一.新建登录名1. 在登录名右侧的文本框中输入新建的管理员账号名称:2. 一对单选按钮组中,选择Sql Server 身份验证,并输入登录密码:3. 勾选强制实施密码策略复选框:(密码策略一般是指加强 ...

  10. 校园网IPv6加速

    对于广大学生来说,上网是一件很纠结的事情,校园网要么按时间计费,要么按流量计费,要么是校园宽带.按时间计费速度慢,按流量计费费用高,校园宽带还不能共享,只能电脑开热点给手机上网.有没有既能提高网速又经 ...