[抄题]:

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. WebGL编程指南案例解析之绘制三个点

    //案例2.绘制3个点,将顶点数据存到缓冲区对象(gl.ARRAY_BUFFER)中,然后顶点着色器从里面读数据(3个顶点) //着色器将对这些顶点进行逐个解析, //第一个顶点给到顶点着色器,赋值给 ...

  2. Java并发编程之三:volatile关键字解析 转载

    目录: <Java并发编程之三:volatile关键字解析 转载> <Synchronized之一:基本使用>   volatile这个关键字可能很多朋友都听说过,或许也都用过 ...

  3. 在webView 中使用JS 调用 Android / IOS的函数 Function

    最近做一个项目,混合了NativeCode 和 HTML,为了便于JS 调用App的一些方法,统一封装一个Js方法,记录如下 Android 端首先要再WebView中允许JS的调用 WebView ...

  4. java模板导出PDF

    本次完善综合特点: 一对一,点对点的给对应的地方写值,比如模板里面放了个name标识,在程序里把“张三”赋给name,那么输出的pdf里面name的地方就变成了张三,准确方便快捷 支持中文,可以使用自 ...

  5. WindowsXamlHost:在 WPF 中使用 UWP 控件库中的控件

    在 WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit) 一文中,我们说到了在 WPF 中引入简单的 UWP 控件以及相关的注意事项 ...

  6. 20165212实验三——敏捷开发与XP实践

    20165212实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验知识点总结 (一)敏捷开发与XP 软件工程:把系统的.有序的.可量化的方法应用到软件的开发.运营和维护上的过 ...

  7. web应用中Filter过滤器之开发应用

    1 过滤器的简单开发应用部署 首先讲解过滤器的开发部署运行基本流程,在这里先通过一个简单的示例: 1)编写过滤器类 编写一个简单的过滤器类:SimpleFilter,实现Filter接口,完整的代码为 ...

  8. 【转】Ubuntu13.04配置:Vim+Syntastic+Vundle+YouCompleteMe

    原文网址:http://www.cnblogs.com/csuftzzk/p/3435710.html 序言 使用Ubuntu和vim已经有一段时间了,对于Vim下的插件应用,我总是抱着一股狂热的态度 ...

  9. 【monkey】mokey常用事件<二>

    格式:adb shell monkey 事件参数 百分数 事件数,如: adb shell monkey --pct-majornav 100 -v 10 --pct-touch <percen ...

  10. java用double和float进行小数计算精度不准确

    java用double和float进行小数计算精度不准确 大多数情况下,使用double和float计算的结果是准确的,但是在一些精度要求很高的系统中或者已知的小数计算得到的结果会不准确,这种问题是非 ...