原题链接在这里: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:

  1. How about using a data structure such as deque (double-ended queue)?
  2. The queue size need not be the same as the window’s size.
  3. 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相关推荐

  1. leetcode 239. Sliding Window Maximum | 239. 滑动窗口最大值(单调栈,窗口内最大最小值更新结构)

    题目 https://leetcode.com/problems/sliding-window-maximum/ 题解 窗口内最大最小值更新结构,单调栈问题,左神视频讲过,<程序员算法面试指南& ...

  2. 239 Sliding Window Maximum 滑动窗口最大值

    给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 例如, 给定 nums = [1,3,-1,- ...

  3. 239. Sliding Window Maximum

    文章目录 1理解题目 2 思路 2.1暴力求解 2.2双端队列 1理解题目 输入:整数数组nums,滑动窗口大小k 输出:整数数组 规则:在一个窗口内只能看到k个数,找一个最大的数,添加到返回数组中. ...

  4. Sliding Window Maximum

    题目 Sliding Window Maximum A long array A[] is given to you. There is a sliding window of size w whic ...

  5. LeetCode 滑动窗口(Sliding Window)类问题总结

    导语 滑动窗口类问题是面试当中的高频题,问题本身其实并不复杂,但是实现起来细节思考非常的多,想着想着可能因为变量变化,指针移动等等问题,导致程序反复删来改去,有思路,但是程序写不出是这类问题最大的障碍 ...

  6. python实现滑动窗口平均_数据流滑动窗口平均值 · sliding window average from data stream...

    [抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...

  7. [Swift]LeetCode480. 滑动窗口中位数 | Sliding Window Median

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. POJ 2823 Sliding Window

    Sliding Window 链接:http://poj.org/problem?id=2823 Time Limit: 12000MS   Memory Limit: 65536K       Ca ...

  9. 【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 ...

最新文章

  1. Fedora 23 将默认使用 Wayland – 多监视器支持
  2. POJ3133(插头dp)
  3. 带有.NET Core App的Identity Server 4
  4. usb 视频设备 按钮消息处理 和普通usb连接的事件处理
  5. 世界是数字的读后感:
  6. 使用JIRA搭建企业问题跟踪系统.PART5(转)
  7. #16192董哥授课的CCNP交换部分总结(三)
  8. 怎么在国内创建谷歌账号_如何在Google相册中创建和共享协作相册
  9. 每日工作记录——W5500网口ping中出现的问题
  10. 《Cocos Creator游戏实战》实现微信小游戏的截图,预览和相册保存功能
  11. (附源码)spring boot学科竞赛活动报名系统 毕业设计 012239
  12. 计算机视觉 || 手写字体检测
  13. 课后作业4:个人总结
  14. 【CVPR20超分】Zooming Slow-Mo: Fast and Accurate One-Stage Space-Time Video Super-Resolution
  15. 基于SSM框架的旅游网站的设计与实现
  16. 企业绩效评价体系的四大层次
  17. 中断实验计算机组成原理,计算机组成原理 中断实验 实验五
  18. 哈工大近世代数期末复习
  19. linux查看达梦数据库信息,DM8 达梦数据库 查看数据库版本号 方法
  20. 有关nano安装Arduino

热门文章

  1. python人脸识别环境搭建_怎样用3分钟搭建 Python 人脸识别系统
  2. Shiro学习记录(详细)
  3. python中使用pickle进行序列化
  4. 机器学习三要素之策略
  5. IP地址、子网掩码、网关、路由器等知识积累
  6. 网易云基于Prometheus的微服务监控实践
  7. 第三课——MFC编程
  8. Linux RHCS中心跳网络接口的指定
  9. uLua Unity工作机制
  10. CentOS 6.4 卸载与安装桌面