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 ...
随机推荐
- 【JAVA】servlet 定时启动
步骤一: web.xml中加上如下的代码: <load-on-startup>10</load-on-startup>这句话是重点. <servlet> <s ...
- 与大V一对一沟通 欢迎迷茫的你和优秀的你
当当当~有个筹备一年的平台终于要发布啦! 在这一年中,他们的专业团队对现行货币市场进行精准分析,了解币圈用户所想的问题以及现在用户最想在平台上能够解决什么样问题后,推出了这样一个平台. 那就是ImCa ...
- ASP.NET Core 3.0预览版体验
目前.NET Core 3.0的版本为.NET Core 3.0 Preview 3,对应ASP.NET Core 3.0 Preview 3. ASP.NET Core 3.0 之后将不再支持.NE ...
- 程序员之路:python3+PyQt5+pycharm桌面GUI开发
http://blog.sina.com.cn/s/blog_989218ad0102wz1k.html 先看效果: 图 1 没错,学过C#的同学应该很熟悉这个界面,按钮风格和界面风格很相似,万万没想 ...
- vim编辑器操作命令
vim [参数] [文件 ..] 编辑指定的文件 或: vim [参数] - 从标准输入(stdin)读取文本 或: vim [参数] -t ...
- 2017-10-29—英语发音的一些技巧总结
学习了这么多年英语还是一句口语也说不出口,大家一定像我一样有hin多的f*k想说. 在很小的时候我们就学了英语音标,知道了有前元音.中元音.后元音(很多同志多年不用应该已经把这些忘得差不多了,like ...
- Android ble蓝牙问题
(1)蓝牙回调安卓4.4的蓝牙回调是在异步线程中(不在主线程),若要在蓝牙回调中执行更新界面的操作,记得切换到主线程去操作 (2)三星手机兼容性问题connectGatt()方法在某些三星手机上只能在 ...
- tp5.0与mysql存储过程
存储过程是一组预编译的sql语句,只需要创建一次过程,以后在程序中就可以调用该过程任意次,执行的速度快于普通sql语句,对于没有权限执行存储过程的用户,也可授权他们执行存储过程,存储过程是保存在数据库 ...
- Windows 2012 下Redmine安装和环境搭建
公司在过去一年中处于高速发展创业期,对于技术管理和项目管理没有找到一个很好的管理工具,使用过Teanbition+禅道+SVM的集成管理工具,但是明显各工具之间联系性差,断层严重,不能很好的形成团队成 ...
- Python基础之元组和字典
一.元组: 1.定义: 内存图: 2.基本操作 3.元组作用: 4.元组基础知识代码 # . 创建空元组 t01 = () t02 = tuple() # . 创建具有默认值的元组 t01 = (,, ...