滑动窗口的最大值 · sliding-window-maximum
[抄题]:
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(递减队列)
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- return result是利用第二次,初始化为0了。return new int[0]是利用第一次,里面没东西。
- 类似于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的更多相关文章
- 滑动窗口协议(Sliding Window Protocol)
滑动窗口协议(Sliding Window Protocol),属于TCP协议的一种应用,用于网络数据传输时的流量控制,以避免拥塞的发生.该协议允许发送方在停止并等待确认前发送多个数据分组.由于发送方 ...
- [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 ...
- Leetcode 239题 滑动窗口最大值(Sliding Window Maximum) Java语言求解
题目链接 https://leetcode-cn.com/problems/sliding-window-maximum/ 题目内容 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧 ...
- 滑动窗口的中位数 · Sliding Window Median
[抄题]: 给定一个包含 n 个整数的数组,和一个大小为 k 的滑动窗口,从左到右在数组中滑动这个窗口,找到数组中每个窗口内的中位数.(如果数组个数是偶数,则在该窗口排序数字后,返回第 N/2 个数字 ...
- 洛谷——P1886 滑动窗口|| POJ——T2823 Sliding Window
https://www.luogu.org/problem/show?pid=1886#sub || http://poj.org/problem?id=2823 题目描述 现在有一堆数字共N个数字( ...
- leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...
- 【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 ...
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
- lintcode 滑动窗口的最大值(双端队列)
题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
随机推荐
- 爸妈才是最好的避孕药--------"北大状元拉黑父母事件的一些感想"
今天看了这么一篇文章,地址: http://mini.eastday.com/mobile/180131180318786.html <北大状元拉黑父母6年:你敢恨爸妈,可你敢原谅他们吗?&g ...
- Map集合统计字母次数
Map集合练习:"asfefxAAcf34vrfdfse2-2asd--wdd"获取该字符串中,每一个字母出现的次数要求打印的结果是:a(2)c(1)...;思路:对结果分析发现, ...
- maven安装之后,或者升级之后遇到的问题:could not find or load main class org.codehaus.plexus.class.....
从maven2升级到maven3或者从maven3降级到maven2,M2_HOME环境变量改变后,在终端执行mvn -v,出现如下错误: Exception in thread "main ...
- C语言 scanf()和gets()函数的区别
C语言 scanf()和gets()函数的区别 1.相同点:scanf( )函数和gets( )函数都可用于输入字符串 2.不同点:两者在功能上有所区别,具体区别如下: 要实现如下需求“从控制台输入字 ...
- TypeScript学习笔记(七) - 命名空间
本篇将介绍TypeScript的命名空间,并简单说明一下与模块的区别. 在之前的例子里,有如下一段代码,通过修改这段代码来演示命名空间的用法. interface Animal { name: str ...
- [C++/Python] 如何在Python中使用一个DLL? (Windows环境)
开发环境VS2012, WIN7 64. 首先生成的DLL大致如下: .h文件 #ifdef CVINPYTHON_EXPORTS #define CVINPYTHON_API __declspec( ...
- RK3288 开机动画旋转
CPU:RK3288 系统:Android 5.1 如果开机动画与屏显示方向不一致,有两种方法可以更改开机动画方向. 一.RK3288默认的开机动画是由两张图片组合而成的,可以直接旋转两张图片的方向. ...
- There is no Action mapped for namespace / and action name login. - [unknown location]
(自己在浏览器中,直接进入项目的根目录,即 http://localhost:8080/ssh/ 时便报错,web.xml文件已经配置了 欢迎页面 <welcome-file-list> ...
- vim自定义配置之nerdTree
vimConfig/plugin/nerdTree-setting.vim let g:NERDTree_title="[NERDTree]" nmap <F2> :N ...
- 黄聪:360浏览器、chrome开发扩展插件教程(1)开发Chrome Extenstion其实很简单
转载:http://www.cnblogs.com/walkingp/archive/2011/03/31/2001628.html Chrome的更新速度可以说前无古人,现在我每天开机的第一件事就是 ...