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. 全文搜索引擎Elasticsearch入门实践

    全文搜索引擎Elasticsearch入门实践 感谢阮一峰的网络日志全文搜索引擎 Elasticsearch 入门教程 安装 首先需要依赖Java环境.Elasticsearch官网https://w ...

  2. UOJ#36. 【清华集训2014】玛里苟斯 线性基

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ36.html 题解 按照 $k$ 分类讨论: k=1 : 我们考虑每一位的贡献.若有至少一个数第 $i$ ...

  3. mybatis循环、mybatis传map

    mybatis中使用循环.mybatis传入map案例 <!-- 根据id修改商户提成配置--> <update id="editStopAll" paramet ...

  4. 安全体系(一)—— DES算法详解

    本文主要介绍了DES算法的步骤,包括IP置换.密钥置换.E扩展置换.S盒代替.P盒置换和末置换. 安全体系(零)—— 加解密算法.消息摘要.消息认证技术.数字签名与公钥证书 安全体系(二)——RSA算 ...

  5. 如何配置.Net Core Centos守护进程配置

    一.安装supervisor 运行命令 yum install supervisor 二.配置supervisor 1.运行命令创建文件夹 mkdir -p /etc/supervisor/conf. ...

  6. Python金融大数据分析PDF

    Python金融大数据分析(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1CF2NhbgpMroLhW2sTm7IJQ 提取码:clmt 复制这段内容后打开百度网盘 ...

  7. Android 基础知识

    system/app   与   system/priv-app Android4.4系统在system目录下新增了priv-app目录,在该目录下的apk一般都是系统核心应用如Launcher.sy ...

  8. Python自学知识点----Day02

    Linux基本操作命令: 命令                                    作用                                     英文释义 ls    ...

  9. C++中的常量函数

    (1)常量成员函数不修改对象. (2)常量成员函数在定义和声明中都需要加上 const; (3)非常量成员函数不能被常量成员函数调用,但构造函数和析构函数除外. (4)常量(cosnt对象)对象不能调 ...

  10. MTCP 在 64 位机器上不工作

    今天打开以前写的 MTCP, 却无法运行. 报错如下: Exception in thread "Thread-0" java.lang.UnsatisfiedLinkError: ...