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 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].
方法1:Time Complexity O(NK)
暂时只有两个Heap的做法,缺点:In this problem, it is necessary to be able remove elements that are not necessarily at the top of the heap. PriorityQueue has logarithmic time remove top, but a linear time remove arbitrary element.
For a Heap:
remove(): Time Complexity is O(logN)
remove(Object): Time Complexity is O(N)
更好的有multiset的方法,但是还没有看到好的java version的
最大堆的简单定义方法:Collections.reverseOrder(), Returns a comparator that imposes the reverse of the natural ordering on a collection of objects
public class Solution {
PriorityQueue<Double> high = new PriorityQueue();
PriorityQueue<Double> low = new PriorityQueue(Collections.reverseOrder()); public double[] medianSlidingWindow(int[] nums, int k) {
double[] res = new double[nums.length-k+1];
int index = 0; for (int i=0; i<nums.length; i++) {
if (i >= k) remove(nums[i-k]);
add((double)nums[i]);
if (i >= k-1) {
res[index++] = findMedian();
}
}
return res;
} public void add(double num) {
low.offer(num);
high.offer(low.poll());
if (low.size() < high.size()) {
low.offer(high.poll());
}
} public double findMedian() {
if (low.size() == high.size()) {
return (low.peek() + high.peek()) / 2.0;
}
else return low.peek();
} public void remove(double num) {
if (num <= findMedian()) {
low.remove(num);
}
else {
high.remove(num);
}
if (low.size() < high.size()) {
low.offer(high.poll());
}
else if (low.size() > high.size()+1) {
high.offer(low.poll());
}
}
}
Leetcode: Sliding Window Median的更多相关文章
- [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 ...
- LeetCode 480. Sliding Window Median
原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description 题目: Median is the middl ...
- 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...
- [LeetCode] 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 ...
- Leetcode: sliding window maximum
August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...
- 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 ...
- 239. [LeetCode ]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 ...
- 滑动窗口的中位数 · Sliding Window Median
[抄题]: 给定一个包含 n 个整数的数组,和一个大小为 k 的滑动窗口,从左到右在数组中滑动这个窗口,找到数组中每个窗口内的中位数.(如果数组个数是偶数,则在该窗口排序数字后,返回第 N/2 个数字 ...
- LintCode "Sliding Window Median" & "Data Stream Median"
Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...
随机推荐
- SQL反模式学习笔记22 伪键洁癖,整理数据
目标:整理数据,使不连续的主键Id数据记录变的连续. 反模式:填充断档的数据空缺. 1.不按照顺序分配编号 在插入新行时,通过遍历表,找到的第一个未分配的主键编号分配给新行,来代替原来自动分配的伪主键 ...
- Python学习——python的常用模块
模块:用一堆代码实现了某个功能的代码集合,模块是不带 .py 扩展的另外一个 Python 文件的文件名. 一.time & datetime模块 import time import dat ...
- 潭州课堂25班:Ph201805201 tornado 项目 第八课 增加喜欢功能(课堂笔记)
tornado 相关说明 新增一个页面,用来做图片收藏, 还要在 account.py 创建一个数据库表,记录用户喜欢的图片,哪些图片用户疯狂传奇 cd 到 项目目录下,执行数据库更新 alembic ...
- hadoop2集群环境搭建
在查询了很多资料以后,发现国内外没有一篇关于hadoop2集群环境搭建的详细步骤的文章. 所以,我想把我知道的分享给大家,方便大家交流. 以下是本文的大纲: 1. 在windows7 下面安装虚拟机2 ...
- 关于在虚拟机上安装ubuntu输入不了中文的问题
打开终端后,无法输入中文,按照网络上的教程 1.安装语言包 System Settings–>Language Support–>Install/Remove Languages 选中ch ...
- 09_ for 练习 _ FlowerNumber
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- ECMA Script 6_Generator
Genertor 是一个普通函数,但是有两个特征: function 关键字 与 函数名之间有一个星号: 函数体内部使用 yield 表达式,定义不同的内部状态(yield 在英语里的意思就是“产出 ...
- crawlspider
Scrapy中CrawSpider 回头看: 之前的代码中,我们有很大一部分时间在寻找下一页的url地址或者是内容的url地址或者是内容的url地址上面,这个过程能更简单一些么? 思路: 1. 从re ...
- jdbc的入门学习
一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...
- Jmeter-----图形扩展监控
Jmeter----图形扩展监控 监听器中插件安装成功如下图: 安装步骤: 1. 下载JMeterPlugins-Extras与JMeterPlugins-Standard,解压缩后在他们各自的\l ...