滑动窗口的最大值 · 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/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
随机推荐
- MySQL 存储过程理解
/********************************************************************************* * MySQL 存储过程理解 * ...
- 掉电脉冲映射串口log和dmesg到文件中的log
1.echo 1 > /mytest/boot_times 2.systemctl enable i2c_dmesg.service root:/mytest# tree . |-- boot_ ...
- 一步搞定私有Git服务器部署(Gogs)
http://www.jianshu.com/p/424627516ef6 零.安装 Docker 和 Compsoe 首先安装 Docker: $ curl -sSL https://get.doc ...
- bzoj2004公交线路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 好美妙的矩阵乘. 思考: 0.在一个序列上.所以考虑dp. 1.p<=10,k& ...
- [C++ Primer] 第9章: 顺序容器
顺序容器概述 顺序容器的类型有: 类型 说明 vector 可变长度数组. 支持快速随机访问. deque 双端队列. 支持快速随机访问. list 双向链表. 只支持双向顺序访问. forward_ ...
- [C++ Primer] 第8章: IO库
IO类 iostream定义了读写流的基本类型 istream, wistream 从流读取数据 ostream, wostream 向流写入数据 iostream, wiostream 读写流 fs ...
- mysql-7事务管理
1.事务的使用场景 mysql事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人愿,你既需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...
- VMware vSphere Client下增加虚拟机磁盘空间的方法
随着系统运维时间的增长,磁盘就日益的损耗,如果遇到虚拟机报磁盘空间不足怎么办?还好,我们可以通过磁盘阵列增加磁盘空间,然后扩容到虚拟机中去. 对于linux虚拟机磁盘扩容的方案有两种,一种就是原有的实 ...
- MTU&MSS
MTU是Maximum Transmission Unit的缩写,意为最大传输单元,通俗的理解就是在网络上传送的最大数据包,单位是字节. 以太网对数据帧的长度都有一个限制,其最大值为1500,这个特性 ...
- PMP Fundamentals