[抄题]:

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

对于数组 [1,2,7,8,5], 滑动大小 k = 3 的窗口时,返回 [2,7,7]

最初,窗口的数组是这样的:

[ | 1,2,7 | ,8,5] , 返回中位数 2;

接着,窗口继续向前滑动一次。

[1, | 2,7,8 | ,5], 返回中位数 7;

接着,窗口继续向前滑动一次。

[1,2, | 7,8,5 | ], 返回中位数 7;

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 不理解两个heap和窗口的大小关系:把窗口容量全扔进来,具体分到哪个格子另当别论
  2. 体会到了treemap相对于heap的优越性:想romove哪个点是随便的。注意接口、实现都不是PQ,是treeset 而且树状的题想想里面装的是node还是数字

[一句话思路]:

窗口移动就是加一个元素、减一个元素,用俩函数实现,所以可以放在maxheap minheap中

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 窗口满了之后romove第一个点,i - k + 1,不是第i个点,写习惯了就错了。重要的参数要提前注释行
  2. 要在maxheap有点的前提下进行节点交换,想到其临界情况:还没有点

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

参数需要边分析边写,留意leetcode lintcode接口是不是不一样

[复杂度]:Time complexity: O(n个数*左右treeset体积lgk) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. node中自己的类、自己的compareTo方法都应该有参数,否则无法调用,要理解其作用
  2. 只有implements能实现接口,还是很多个。不要写extends

[关键模板化代码]:

 自制Collections.sort 方法有一个字母 第一位不相等
 自制compareTo 方法有两个字母 第二位相等

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

class Node implements Comparable<Node>{
int id;
int val;
Node (int id, int val){
this.id = id;
this.val = val;
}
public int compareTo(Node other) {
Node a = other;
if (this.val == a.val) {
return this.id - a.id;
}else {
return this.val - a.val;
}
}
} public class Solution {
/*
* @param nums: A list of integers
* @param k: An integer
* @return: The median of the element inside the window at each moving
*/
public double[] medianSlidingWindow(int[] nums, int k) {
//corner case
int n = nums.length;
double[] result = new double[n];
if (nums == null || k == 0) {
return result;
}
TreeSet<Node> minHeap = new TreeSet<>();
TreeSet<Node> maxHeap = new TreeSet<>();
//add all nums into window, rest
int half = (k + 1) / 2;
int index = 0;
for (int i = 0; i < k - 1; i++) {
add(minHeap, maxHeap, half, new Node(i, nums[i]));
}
for (int i = k - 1; i < n; i++) {
add(minHeap, maxHeap, half, new Node(i, nums[i]));
nums[index] = minHeap.last().val;
index++;
remove(minHeap, maxHeap, new Node(i - k + 1, nums[i - k + 1]));
} return result;
} // write reference first!
void add(TreeSet<Node> minHeap, TreeSet<Node> maxHeap, int size, Node node) {
if (minHeap.size() < size) {
minHeap.add(node);
}else {
maxHeap.add(node);
} if (minHeap.size() == size) {
//don't forget just minheap, need to ensure
if (maxHeap.size() > 0 && minHeap.last().val > maxHeap.first().val) {
Node b = minHeap.last();
Node s = maxHeap.first();
minHeap.remove(b);
minHeap.add(s);
maxHeap.remove(s);
maxHeap.add(b);
}
}
} void remove(TreeSet<Node> minHeap, TreeSet<Node> maxHeap, Node node) {
if (minHeap.contains(node)) {
minHeap.remove(node);
}else {
maxHeap.remove(node);
}
}
}

[代码风格] :

  1. 打草稿的时候先把函数参数写了 是分析出来的,不要主函数调用的时候就瞎写
  2. Node 注意开头得大写

滑动窗口的中位数 · Sliding Window Median的更多相关文章

  1. 滑动窗口协议(Sliding Window Protocol)

    滑动窗口协议(Sliding Window Protocol),属于TCP协议的一种应用,用于网络数据传输时的流量控制,以避免拥塞的发生.该协议允许发送方在停止并等待确认前发送多个数据分组.由于发送方 ...

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

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

  3. 洛谷——P1886 滑动窗口|| POJ——T2823 Sliding Window

    https://www.luogu.org/problem/show?pid=1886#sub || http://poj.org/problem?id=2823 题目描述 现在有一堆数字共N个数字( ...

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

  5. Lintcode360 Sliding Window Median solution 题解

    [题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...

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

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

  8. LeetCode 480. Sliding Window Median

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

  9. 480 Sliding Window Median 滑动窗口中位数

    详见:https://leetcode.com/problems/sliding-window-median/description/ C++: class Solution { public: ve ...

随机推荐

  1. WebGL编程指南案例解析之绘制三角形

    //案例3.绘制三角形,将顶点数据存到缓冲区对象(gl.ARRAY_BUFFER)中,然后顶点着色器从里面读数据(3个顶点) //顶点着色器中去掉gl_PointSize = 10.0,绘制三角不能设 ...

  2. Java第二次作业--数组和String类

    Deadline: 2017-3-28 23:00 一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握基本数据类型和引用数据类型的区别 理解对象的生成与引用的关系 掌握构造方法的重载 掌握St ...

  3. O​r​a​c​l​e​ ​D​a​t​a​b​a​s​e​ ​e​x​p​r​e​s​s​ ​1​1​g​ ​第​ ​2​ ​版​安​装和配置

    官方Oracle Database 快捷版 11g 第 2 版的下载地址: http://www.oracle.com/technetwork/cn/products/express-edition/ ...

  4. nyoj A+B Problem IV

    A+B Problem IV 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 acmj最近发现在使用计算器计算高精度的大数加法时很不方便,于是他想着能不能写个程序把这 ...

  5. FastAdmin 的前端环境怎么安装?

    FastAdmin 的前端环境怎么安装? 安装 Git 安装 Node.js 安装 cnpm 安装 bower 开始安装 FastAdmin 的前端组件 bower install bower upd ...

  6. 纯php实现中秋博饼游戏(2):掷骰子并输出结果

    这篇是纯php实现中秋博饼游戏系列博文(2) 上文是:纯php实现中秋博饼游戏(1):绘制骰子图案 http://www.cnblogs.com/zqifa/p/php-dice-1.html要纯ph ...

  7. 打印进度条——(progress bar才是专业的)

    # 打印进度条——(progress bar是专业的) import time for i in range(0,101,2): time.sleep(0.1) char_num = i//2 #打印 ...

  8. 【python】使用Python中的urlparse、urllib抓取和解析网页

    一.解析URL 函数urlparse(urlstring [, default_scheme [, allow_fragments]])的作用是将URL分解成不同的组成部分,它从urlstring中取 ...

  9. Linux操作系统-基本命令(二)

    Linux操作系统基本命令 文件操作类命令 – ln命令 另外一种链接方式称为符号链接(软链接),是指一个文件指向另外一个文件的文件名.软链接类似于Windows系统中的快捷方式.软链接由ln -s命 ...

  10. Python的设计哲学

    Beautiful is better than ugly. 优美胜于丑陋 Explicit is better than implicit. 明了胜于晦涩 Simple is better than ...