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的更多相关文章

  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 480. Sliding Window Median

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

  3. 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...

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

  5. Leetcode: sliding window maximum

    August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...

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

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

  8. 滑动窗口的中位数 · Sliding Window Median

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

  9. LintCode "Sliding Window Median" & "Data Stream Median"

    Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...

随机推荐

  1. 一起学HBase——简单介绍HBase各种组件

    HBase是谷歌BigTble的开源实现.谷歌的三篇论文拉开了大数据江湖的序幕,铸就了现在以Hadoop为主的大数据技术生态圈.而HBase是开源的大数据数据库,和传统的行式数据库不同的是,HBase ...

  2. 写给Android开发者的Kotlin入门

    写给Android开发者的Kotlin入门 转 https://www.jianshu.com/p/bb53cba6c8f4 Google在今年的IO大会上宣布,将Android开发的官方语言更换为K ...

  3. c#一步一步实现ORM(二)

    c#一步一步实现ORM(二) 上一篇描述了简单的思路,这一片我们来稍微细化一下 1插入的时候忽略某些字段 public int Insert<T>(T o, params string[] ...

  4. CentOS6.5安装图形用户界面

    CentOS 6.5 安装图形界面 安装的时候没有安装图像界面.安装步骤如下: 1.yum -y groupinstall Desktop 2.yum -y groupinstall "X ...

  5. SEED实验——Environment Variable and Set-UID Program实验描述与实验任务

    第一部分:实验描述 该实验的学习任务是理解环境变量是如何影响程序和系统行为的.环境变量是一组动态命名的变量 第二部分:实验任务 2.1 任务一:操作环境变量 在这个任务中,我们研究可以用来设置和取消设 ...

  6. Hibernate 的hql查询简介【申明:来源于网络】

    Hibernate 的hql查询简介[申明:来源于网络] Hibernate 的hql查询简介:http://blog.csdn.net/leaf_130/article/details/539329 ...

  7. 逆向工程-真码保存在系统文件破解QQ游戏对对碰助手

    1)注册栏中输入任意值测试 1.2)记录弹出的关键字对话框 2.1)发送至PEID进行查壳 2.2)发现无壳 2.3)将软件载入OllyDBG程序 2.4)在反汇编栏下右键选择中文搜索引擎->智 ...

  8. raycast 一小段距离碰撞到的poly

    dtNavMeshQuery::raycast(dtPolyRef startRef, const float* startPos, const float* endPos, const dtQuer ...

  9. oo第四次总结

    1.论述测试与正确性论证的效果差异,比较其优缺点 测试:通过大量测试样例覆盖测试代码,来检测代码功能的实现是否正确是否完善.正确性论证:通过对代码规格和逻辑的严密分析,推论和证明,来验证代码实现的正确 ...

  10. vim删除.swp

    非正常关闭vi编辑器时会生成一个.swp文件 关于swp文件 使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一个文件,vi就会生成这么一个.(filename)swp文件以备 ...