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

    01.if ''' 제어문 = 조건문(if) + 반복문(while, for) 조건문 기본 형식1) python 블럭 if 조건식 : 실행문 실행문 cf) c언어 블럭 if 조건식 { ...

  2. 记一次Nginx+Keepalived高可用故障转移

    Master端:192.168.2.156 ! Configuration File for keepalived global_defs { notification_email { acassen ...

  3. 【AtCoder】【组合数学】【模型转换】Colorful Balls(AGC012)

    题意: 有n个球,每个球有两个值,一个是颜色,另一个是重量.可以进行如下的操作任意次: 1.选择两个颜色相同的球,如果这两个球的重量之和小于等于X,就交换这两个球: 2.选择两个颜色不同的球,如果这两 ...

  4. HDU.5385.The path(构造)

    题目链接 最短路构造题三连:这道题,HDU4903,SRM590 Fox And City. \(Description\) 给定一张\(n\)个点\(m\)条边的有向图,每条边的边权在\([1,n] ...

  5. 今天圆满了----tensorflow安装日志

    Ubuntu的安装 不能用easyBCD,因为新电脑是UEFI启动模式,制作了Ubuntu的系统盘,强烈建议只分swap区(2G)和根目录/区,之前因为/home区不够导致重装 建议使用官方安装文档, ...

  6. 封装的head

    //获取浏览器和版本号var userAgent=window.navigator.userAgent, rMsie=/(msie\s|trident.*rv:)([\w.]+)/, rFirefox ...

  7. Java-IO流之输入输出流基础示例

    一.理论: 1.什么是输入输出? 输入输出的对象是数据,数据的存储区域是磁盘或者光盘等设备,我们知道还有一个存储数据的空间----内存,其中磁盘的速度比较慢,内存的速度比较快,把数据读入内存的动作称作 ...

  8. GMA Round 1 数列与方程

    传送门 数列与方程 首项为1,各项均大于0的数列{$a_n$}的前n项和$S_n$满足对于任意正整数n:$S_{n+1}^2-2*S_{n+1}*S_{n}-\sqrt{2}*S_n-1=0$,求$a ...

  9. 11_ for 练习 _ Math.sqrt

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. Java课程课后作业之19学期之第一周博客作业

    作为一个大二的学生,自己已经不小了,没有大一那个时候的无忧无虑的可以放纵的时光,只剩下一年,我就该做出我人生的下一个重大决定了,这一次真的是我一个人的决定,从小到大,父母为我做过很多的决定,即使在小的 ...