题目

https://leetcode.com/problems/sliding-window-maximum/

题解

窗口内最大最小值更新结构,单调栈问题,左神视频讲过,《程序员算法面试指南》也有此题目。

本题的关键在于利用双端队列来实现窗口最大值的更新。

class Solution {public int[] maxSlidingWindow(int[] nums, int k) {Deque<Integer> valueQueue = new LinkedList<>();Deque<Integer> indexQueue = new LinkedList<>();int[] result = new int[nums.length - k + 1];for (int i = 0; i < nums.length; i++) {if (!indexQueue.isEmpty() && indexQueue.peekFirst() <= i - k) {valueQueue.pollFirst();indexQueue.pollFirst();}while (!valueQueue.isEmpty() && valueQueue.peekLast() < nums[i]) {valueQueue.pollLast();indexQueue.pollLast();}valueQueue.offer(nums[i]);indexQueue.offer(i);if (i - k + 1 >= 0) result[i - k + 1] = valueQueue.peekFirst();}return result;}
}

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

  1. LeetCode 239. Sliding Window Maximum

    原题链接在这里:https://leetcode.com/problems/sliding-window-maximum/ 题目: Given an array nums, there is a sl ...

  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 435. Non-overlapping Intervals | 435. 无重叠区间(单调栈)

    题目 https://leetcode.com/problems/non-overlapping-intervals/ 题解 我一开始是没有 get 到这道题的精髓的.只是在想,用贪心法,当两个 (a ...

  6. LeetCode 1944. 队列中可以看到的人数(单调栈)

    文章目录 1. 题目 2. 解题 1. 题目 有 n 个人排成一个队列,从左到右 编号为 0 到 n - 1 . 给你以一个整数数组 heights ,每个整数 互不相同,heights[i] 表示第 ...

  7. LeetCode 1793. 好子数组的最大分数(单调栈)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个整数数组 nums (下标从 0 开始)和一个整数 k . 一个子数组 (i, j) 的 分数 定义为 min(nums[i], nums[i+1 ...

  8. LeetCode 1124. 表现良好的最长时间段(单调栈/哈希)

    文章目录 1. 题目 2. 解题 2.1 单调栈 2.2 哈希 1. 题目 给你一份工作时间表 hours,上面记录着某一位员工每天的工作小时数. 我们认为当员工一天中的工作小时数大于 8 小时的时候 ...

  9. LeetCode 581. 最短无序连续子数组(排序单调栈)

    文章目录 1. 题目 2. 解题 2.1 排序 2.2 4次遍历 2.3 单调栈 1. 题目 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. ...

最新文章

  1. CORBA/DCOM使用协议
  2. Navicate Premium不能用localhost和127.0.0.1登陆sql-server,解决方法
  3. 【完美】SpringBoot中使用注解来实现 Redis 分布式锁
  4. Python No Module name cv2解决方案
  5. ASP.Net学习笔记012--12ViewState初探
  6. perl - Java调用perl
  7. 地理信息数据 中国市级行政区划 SHP
  8. hutool实战(带你掌握里面的各种工具)目录
  9. docker安装speedtest和宝塔面板
  10. 收藏的兼容各浏览器的日历控件(ie6-11\ff\google\safri)
  11. No certificate for team ‘‘ matching ‘iPhone Distribution: VOVA TECH LIMITED ()‘ Select a different s
  12. Ardunio开发实例-简单声音感应控制开关
  13. JVM-由常量池 运行时常量池 String intern方法想到的(三)之String内存模型
  14. 仿京东详情页商品图片查看
  15. arduino dht11 传感器实现
  16. Yeelight LED智能灯泡(彩光版)代码控制(含pdf资料)
  17. 《中国主要城市道路网密度监测报告》正式发布
  18. 为什么大学毕业生工作难找?
  19. 流量抑制及风暴控制配置命令
  20. L2TP的windows客户端连接

热门文章

  1. CodeForces - 1350E Orac and Game of Life(bfs)
  2. POJ - 3764 The xor-longest Path(字典树性质)
  3. 中石油训练赛 - Racing Gems(最长不下降子序列)
  4. HDU - 5017 Ellipsoid(三分套三分/模拟退火)
  5. POJ - 1655 Balancing Act(树的重心)
  6. HDU4514(非连通图的环判断与图中最长链)
  7. ElasticSearch探索之路(一)初识ElasticSearch:特点、应用场景、架构设计、基本概念
  8. 终于完全弄懂了KMP(个人理解篇)
  9. spark太基础了,今天聊下阿里 2 面必问的数据中台
  10. 25个实用编程小技巧