[抄题]:

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.

For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.

Window position                Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
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 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7

Therefore, return the max sliding window as [3,3,5,5,6,7].

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

[一句话思路]:

左边括号移动挤掉大的,右边挤掉小的。所以两边都要开口,用deque(递减队列)

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

[画图]:

[一刷]:

  1. return result是利用第二次,初始化为0了。return new int[0]是利用第一次,里面没东西。
  2. 类似于heap,deque取最大值都是用peek()

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

取最大值都要用peek()

[复杂度]:Time complexity: O(并非每个元素都要一进一出,<2n) Space complexity: O(n)

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

deque的实现是arraydeque

[关键模板化代码]:

[其他解法]:

[Follow Up]:

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

76. Minimum Window Substring 哈希表,还挺复杂

[代码风格] :

class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
//corner case
int[] result = new int[nums.length - k + 1];
int index = 0;
if (nums == null || k <= 0) {
return new int[0];
}
Deque<Integer> q = new ArrayDeque<Integer>(); for (int i = 0; i < nums.length; i++) {
//get nums out of range k
while (!q.isEmpty() && i - k + 1 > q.peek()) {
q.poll();
}
//get nums smaller
while (!q.isEmpty() && nums[i] > nums[q.peekLast()]) {
q.pollLast();
}
//get nums bigger, add to result
q.offer(i);
if (i >= k - 1) {
result[index++] = nums[q.peek()];
}
} return result;
}
}

滑动窗口的最大值 · sliding-window-maximum的更多相关文章

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

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

  2. [Swift]LeetCode239. 滑动窗口最大值 | 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 ...

  3. Leetcode 239题 滑动窗口最大值(Sliding Window Maximum) Java语言求解

    题目链接 https://leetcode-cn.com/problems/sliding-window-maximum/ 题目内容 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧 ...

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

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

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

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

  6. leetcode面试准备:Sliding Window Maximum

    leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...

  7. 【LeetCode 239】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. 【LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum   Given an array nums, there is a sliding window of size k which is moving fr ...

  9. 【刷题-LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...

  10. lintcode 滑动窗口的最大值(双端队列)

    题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为  ...

随机推荐

  1. vue url生产二维码

    <template> <div id="QRcode"> <div class='QR-qrcode' style='display:none;'&g ...

  2. 《次元唤醒 需求规格说明书v1.0》

    一.团队分工 组员 工作比例 参与范围 王诚荣 17% 原型设计,需求规格说明书整合,LOGO设计 马祎特 22% PPT制作,演讲,博客模板,用户描述 陈斌 21% 评审表格制作,引言,项目描述,功 ...

  3. I.MX6 eMMC 中启动U-boot存放的首地址

    /************************************************************************************ * I.MX6 eMMC 中 ...

  4. 区间DP的摸索

    (poj真的炸了,以下代码可能有误) 按照下面这个做题顺序,对区间DP不再那么迷了 LOJ1422 是 dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j])而不是d ...

  5. L3-012 水果忍者 (30 分)

    2010年风靡全球的“水果忍者”游戏,想必大家肯定都玩过吧?(没玩过也没关系啦~)在游戏当中,画面里会随机地弹射出一系列的水果与炸弹,玩家尽可能砍掉所有的水果而避免砍中炸弹,就可以完成游戏规定的任务. ...

  6. JavaWeb入门环境搭建

    一.安装配置Tomcat 1.下载 2.配置环境变量 配置JAVA_HOME环境变量,路径为JDK的根目录 3.测试Tomcat 打开浏览器,在地址栏输入http://localhost:8080可以 ...

  7. nexus yum 私服集成

      nexus 集成了 yum 私服使用起来还是比较简单的 配置 yum proxy 实际使用我们可能需要配置centos 以及epel 的源 centos可以用http://mirror.cento ...

  8. 使用位图文本工具BMFont从图片生成自定义字体

    bmfont工具如何使用 http://www.360doc.com/content/13/1206/12/14253074_334930801.shtml fnt各属性含义 http://www.2 ...

  9. Hystrix已经停止开发,官方推荐替代项目Resilience4j

    随着微服务的流行,熔断作为其中一项很重要的技术也广为人知.当微服务的运行质量低于某个临界值时,启动熔断机制,暂停微服务调用一段时间,以保障后端的微服务不会因为持续过负荷而宕机.本文介绍了新一代熔断器R ...

  10. bzoj 5020(洛谷4546) [THUWC 2017]在美妙的数学王国中畅游——LCT+泰勒展开

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5020 https://www.luogu.org/problemnew/show/P4546 ...