LeetCode 239. Sliding Window Maximum
原题链接在这里:https://leetcode.com/problems/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 31 [3 -1 -3] 5 3 6 7 31 3 [-1 -3 5] 3 6 7 51 3 -1 [-3 5 3] 6 7 51 3 -1 -3 [5 3 6] 7 61 3 -1 -3 5 [3 6 7] 7
Therefore, return the max sliding window as [3,3,5,5,6,7]
.
Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.
Follow up:
Could you solve it in linear time?
Hint:
- How about using a data structure such as deque (double-ended queue)?
- The queue size need not be the same as the window’s size.
- Remove redundant elements and the queue should store only elements that need to be considered.
题解:
用deque, 里面存index.
从尾部添加index前先检查deque的尾部index对应的元素nums[deque.getLast()]是否比要添加的元素nums[i]小或者相等,若是,就把尾部index remove掉,一直remove直到遇到比nums[i]大的数或者LinkedList 为空。e.g. 当添加nums[1] = 3的index 1时,最大的数肯定是3,1就没有用了。也就是说如果出现比先添加的数大的数时,先添加的就没有用了。
如此deque里面保存的就是[第一大index, 第二大index, 第三大index, 第四大index...].
若是i - 头部的index >= k, 就说明现在的window大小已经大于了k, 就需要从头remove一次.
当 i+1>=k 是开始记录res. res的坐标为i-k+1, 取ls的头index, 也就是当前窗口的最大index. 把对应的元素加大res中。
Time Complexity: O(n). 每个元素最多进deque一次, 出deque一次. Space O(k).
AC Java:
1 public class Solution { 2 public int[] maxSlidingWindow(int[] nums, int k) { 3 if(k == 0){ 4 return new int[0]; 5 } 6 7 int [] res = new int[nums.length-k+1]; 8 LinkedList<Integer> deque = new LinkedList<Integer>(); 9 for(int i = 0; i<nums.length; i++){ 10 while(!deque.isEmpty() && nums[deque.getLast()]<=nums[i]){ 11 deque.removeLast(); 12 } 13 deque.addLast(i); 14 if(i - deque.getFirst() >= k){ 15 deque.removeFirst(); 16 } 17 if(i+1>=k){ 18 res[i+1-k] = nums[deque.getFirst()]; 19 } 20 } 21 return res; 22 } 23 }
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4938106.html
LeetCode 239. Sliding Window Maximum相关推荐
- leetcode 239. Sliding Window Maximum | 239. 滑动窗口最大值(单调栈,窗口内最大最小值更新结构)
题目 https://leetcode.com/problems/sliding-window-maximum/ 题解 窗口内最大最小值更新结构,单调栈问题,左神视频讲过,<程序员算法面试指南& ...
- 239 Sliding Window Maximum 滑动窗口最大值
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 例如, 给定 nums = [1,3,-1,- ...
- 239. Sliding Window Maximum
文章目录 1理解题目 2 思路 2.1暴力求解 2.2双端队列 1理解题目 输入:整数数组nums,滑动窗口大小k 输出:整数数组 规则:在一个窗口内只能看到k个数,找一个最大的数,添加到返回数组中. ...
- Sliding Window Maximum
题目 Sliding Window Maximum A long array A[] is given to you. There is a sliding window of size w whic ...
- LeetCode 滑动窗口(Sliding Window)类问题总结
导语 滑动窗口类问题是面试当中的高频题,问题本身其实并不复杂,但是实现起来细节思考非常的多,想着想着可能因为变量变化,指针移动等等问题,导致程序反复删来改去,有思路,但是程序写不出是这类问题最大的障碍 ...
- python实现滑动窗口平均_数据流滑动窗口平均值 · sliding window average from data stream...
[抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...
- [Swift]LeetCode480. 滑动窗口中位数 | Sliding Window Median
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- POJ 2823 Sliding Window
Sliding Window 链接:http://poj.org/problem?id=2823 Time Limit: 12000MS Memory Limit: 65536K Ca ...
- 【POJ - 2823】 Sliding Window(单调队列 用双端队列实现或模拟队列)
题干: An array of size n ≤ 10 6 is given to you. There is a sliding window of size k which is moving f ...
最新文章
- Fedora 23 将默认使用 Wayland – 多监视器支持
- POJ3133(插头dp)
- 带有.NET Core App的Identity Server 4
- usb 视频设备 按钮消息处理 和普通usb连接的事件处理
- 世界是数字的读后感:
- 使用JIRA搭建企业问题跟踪系统.PART5(转)
- #16192董哥授课的CCNP交换部分总结(三)
- 怎么在国内创建谷歌账号_如何在Google相册中创建和共享协作相册
- 每日工作记录——W5500网口ping中出现的问题
- 《Cocos Creator游戏实战》实现微信小游戏的截图,预览和相册保存功能
- (附源码)spring boot学科竞赛活动报名系统 毕业设计 012239
- 计算机视觉 || 手写字体检测
- 课后作业4:个人总结
- 【CVPR20超分】Zooming Slow-Mo: Fast and Accurate One-Stage Space-Time Video Super-Resolution
- 基于SSM框架的旅游网站的设计与实现
- 企业绩效评价体系的四大层次
- 中断实验计算机组成原理,计算机组成原理 中断实验 实验五
- 哈工大近世代数期末复习
- linux查看达梦数据库信息,DM8 达梦数据库 查看数据库版本号 方法
- 有关nano安装Arduino