[抄题]:

给定一个包含 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

[关键模板化代码]:

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

[其他解法]:

[Follow Up]:

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

  1. class Node implements Comparable<Node>{
  2. int id;
  3. int val;
  4. Node (int id, int val){
  5. this.id = id;
  6. this.val = val;
  7. }
  8. public int compareTo(Node other) {
  9. Node a = other;
  10. if (this.val == a.val) {
  11. return this.id - a.id;
  12. }else {
  13. return this.val - a.val;
  14. }
  15. }
  16. }
  17.  
  18. public class Solution {
  19. /*
  20. * @param nums: A list of integers
  21. * @param k: An integer
  22. * @return: The median of the element inside the window at each moving
  23. */
  24. public double[] medianSlidingWindow(int[] nums, int k) {
  25. //corner case
  26. int n = nums.length;
  27. double[] result = new double[n];
  28. if (nums == null || k == 0) {
  29. return result;
  30. }
  31. TreeSet<Node> minHeap = new TreeSet<>();
  32. TreeSet<Node> maxHeap = new TreeSet<>();
  33. //add all nums into window, rest
  34. int half = (k + 1) / 2;
  35. int index = 0;
  36. for (int i = 0; i < k - 1; i++) {
  37. add(minHeap, maxHeap, half, new Node(i, nums[i]));
  38. }
  39. for (int i = k - 1; i < n; i++) {
  40. add(minHeap, maxHeap, half, new Node(i, nums[i]));
  41. nums[index] = minHeap.last().val;
  42. index++;
  43. remove(minHeap, maxHeap, new Node(i - k + 1, nums[i - k + 1]));
  44. }
  45.  
  46. return result;
  47. }
  48.  
  49. // write reference first!
  50. void add(TreeSet<Node> minHeap, TreeSet<Node> maxHeap, int size, Node node) {
  51. if (minHeap.size() < size) {
  52. minHeap.add(node);
  53. }else {
  54. maxHeap.add(node);
  55. }
  56.  
  57. if (minHeap.size() == size) {
  58. //don't forget just minheap, need to ensure
  59. if (maxHeap.size() > 0 && minHeap.last().val > maxHeap.first().val) {
  60. Node b = minHeap.last();
  61. Node s = maxHeap.first();
  62. minHeap.remove(b);
  63. minHeap.add(s);
  64. maxHeap.remove(s);
  65. maxHeap.add(b);
  66. }
  67. }
  68. }
  69.  
  70. void remove(TreeSet<Node> minHeap, TreeSet<Node> maxHeap, Node node) {
  71. if (minHeap.contains(node)) {
  72. minHeap.remove(node);
  73. }else {
  74. maxHeap.remove(node);
  75. }
  76. }
  77. }

[代码风格] :

  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. CF1082E:E.increasing Frequency(贪心&最大连续和)

    You are given array a a of length n n . You can choose one segment [l,r] [l,r] (1≤l≤r≤n 1≤l≤r≤n ) an ...

  2. StringUtils.isEmpty()和isBlank()的区别

    一.概述 两种判断字符串是否为空的用法都是在程序开发时常用的,相信不少同学在这种简单的问题上也吃过亏,到底有什么区别,使用有什么讲究,带着问题往下看. 二.jar包 commons-lang3-3.5 ...

  3. centos7上docker安装和使用教程

    Docker 是一个创建和管理 Linux 容器的开源工具.容器就像是轻量级的虚拟机,并且可以以毫秒级的速度来启动或停止.Docker 帮助系统管理员和程序员在容器中开发应用程序,并且可以扩展到成千上 ...

  4. CentOS 6.5 下搭建FastDFS服务

    参考网站: http://www.open-open.com/lib/view/open1435468300700.html http://blog.csdn.net/lynnlovemin/arti ...

  5. security自动登陆

    package*.security; import java.util.ArrayList; import javax.servlet.http.Cookie; import javax.servle ...

  6. python 书籍推荐 一

    最近"瑞丽模特学Python"的热点牵动了大江南北程序员的心,有人说这是炒作,也有人说这是推广Python的爆点...我嘿嘿一笑,美女就是美女,眼光那是杠杠的,不仅人美,学的语言也 ...

  7. VS编译后事件

    拷贝文件 copy /y "$(OutDir)Maticsoft.BuilderDALParam.dll" "D:\Project Area\2016-3-28" ...

  8. 关于OkHttp–支持SPDY协议的高效HTTP库 com.squareup.okhttp

    转载:http://liuzhichao.com/p/1707.html OkHttp–支持SPDY协议的高效HTTP库 柳志超博客 » Program » Andriod » OkHttp–支持SP ...

  9. VPS(Centos6)连ROS做GRE隧道完整版

    国内徐庄1.1.1.1     内网地址192.168.0.0/16  gre隧道适配器地址 172.16.0.45 国外2.2.2.2内网地址无 gre隧道地址172.16.0.46 国外 cent ...

  10. Oracle Database PSU/CPU

    1. 什么是PSU/CPU?CPU: Critical Patch UpdateOracle对于其产品每个季度发行一次的安全补丁包,通常是为了修复产品中的安全隐患. PSU: Patch Set Up ...