滑动窗口的最大值 · 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/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
随机推荐
- 使用c++实现一个FTP客户端(三)
接上篇:http://www.cnblogs.com/jzincnblogs/p/5217688.html,这篇主要记录编程过程中需要注意的地方以及遇到的一些问题及解决方法. 一.gethostbyn ...
- BusyBox ifup udhcpc后台运行
/********************************************************************** * BusyBox ifup udhcpc后台运行 * ...
- vi/vim使用进阶: 在VIM中使用GDB调试 – 使用vimgdb
vi/vim使用进阶: 在VIM中使用GDB调试 – 使用vimgdb << 返回vim使用进阶: 目录 本节所用命令的帮助入口: :help vimgdb 在UNIX系统最初设计时,有一 ...
- LG3781 [SDOI2017]切树游戏
题意 题目描述 小Q是一个热爱学习的人,他经常去维基百科学习计算机科学. 就在刚才,小Q认真地学习了一系列位运算符,其中按位异或的运算符\(\oplus\)对他影响很大.按位异或的运算符是双目运算符. ...
- 对ashx请求用Gzip,Deflated压缩
//GZIP压缩 //查看请求头部 string acceptEncoding = context.Request.Headers["Accept-Encoding"].ToStr ...
- Spring AOP 实现读写分离
原文地址:Spring AOP 实现读写分离 博客地址:http://www.extlight.com 一.前言 上一篇<MySQL 实现主从复制> 文章中介绍了 MySQL 主从复制的搭 ...
- erp中三大订单CO、PO、MO
ERP即 企业资源计划 (Enterprise Resource Planning),由美国 Gartner Group 公司于1990年提出. ERP系统是指建立在信息技术基础上,以系统化的管理思想 ...
- 黄聪:WordPress 多站点建站教程(二):后台(管理网络)设置详解,如何管理子站的用户、主题、插件、设置等功能
建立好了子站,我们需要有个地方配置所有子站的主题.插件等功能,我们可以在后台看到 我的站点--管理网络 如下图: 在 管理网络--仪表盘 里面,我们可以创新用户和站点,也提供了查询功能. 要注意的是: ...
- JAVA面向对象编程课程设计——web版斗地主
一.团队课程设计博客链接 JAVA面向对象编程课程设计--网络版单机斗地主 二.个人负责模块或任务说明 实体类的设计 斗地主规则的实现 人机自动出牌的算法 实现数据库的DAO模式 三.自己的代码提交记 ...
- ThinkJava-标准IO
1 从标准输入中读取 按照标准1/0模型, Java提供了System.in.System.out和System.err.在整本书里,我们已经 看到了怎样用System.out将数据写出到标准输出,其 ...