题目:

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].

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?


解:

维护一个双端队列保存可能成为当前窗口最大值的所有候选值的索引,队列中的索引表示的值递减,即队列第一个元素为队列中最大值

对于题目给的例子:

首先进队数组中第一个元素1的索引0,当到达第二个值3时,出队索引0,因为其代表的值1不可能成为窗口中的最大值(因为比3小),

到达第三个值-1,比当前队列中索引代表的值3小,必须进队,因为等下如果3从左侧滑出窗口,-1可能成为窗口的最大值。

dq: (1)  3  -1  -3  5(先出队3(超出窗口),接着出队队尾的-1,-3(小于5))

Java代码:

public class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums == null || nums.length == 0)
return nums;
int len = nums.length;
int[] result = new int[len-k+1]; //结果数组
int ri = 0;
//存储递减值的索引的队列
Deque<Integer> dq = new ArrayDeque<Integer>();
for(int i=0; i<len; i++){
//出队超出窗口的索引
if(!dq.isEmpty() && dq.peek() < i-k+1)
dq.poll();
//当窗口末尾即队列尾的数比当前滑进的数小,则出队
while(!dq.isEmpty() && nums[dq.peekLast()] < nums[i])
dq.pollLast();
dq.offer(i);
//窗口满数时,把最大值加入结果数组
if(i >= k-1)
result[ri++] = nums[dq.peek()];
}
return result;
}
}

Python代码:

class Solution:
# @param {integer[]} nums
# @param {integer} k
# @return {integer[]}
def maxSlidingWindow(self, nums, k):
d = collections.deque()
re = []
for i, v in enumerate(nums):
while d and nums[d[-1]] < v:
d.pop()
d.append(i)
if d[0] == i-k:
d.popleft()
if i >= k-1:
re.append(nums[d[0]])
return re

(heap)239. Sliding Window Maximum的更多相关文章

  1. 【LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum   Given an array nums, there is a sliding window of size k which is moving fr ...

  2. 【刷题-LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...

  3. 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  4. 239. Sliding Window Maximum

    题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...

  5. [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 ...

  6. [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 ...

  7. 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...

  8. leetcode 239 Sliding Window Maximum

    这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...

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

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

随机推荐

  1. puppet cert maintain

  2. jQuery旋转插件jqueryrotate 图片旋转

    "jquery.rotate.min.js"是jQuery旋转rotate插件,支持Internet Explorer 6.0+ .Firefox 2.0 .Safari 3 .O ...

  3. c语言const

    const关键字 const和指针结合,共有4种形式 const int *p; p是一个指针,指针指向一个int型数据.p所指向的是个常量. int const *p; p是一个指针,指针指向一个i ...

  4. 讲解版的自动轮播(新手福利)样式和js就不分离了为了看的方便

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. Laravel-数据库操作笔记

    (慕课网_轻松学会Laravel-基础篇_天秤vs永恒老师) 一.直接使用sql语句 1.路由 2.StudentController.php 二.查询构造器 简介:Laravel查询构造器(quer ...

  6. iPhone应用提交流程:如何将App程序发布到App Store?

    对于刚加入iOS应用开发行列的开发者来说,终于经过艰苦的Coding后完成了第一个应用后最重要的历史时刻就是将应用程序提交到iTunes App Store.Xcode 4.2开发工具已经把App提交 ...

  7. OpenWrt sscanf问题之于MT7620N与AR9341

    在MT7620N平台做好了wifidog的相关调试工作,除了eth驱动.wireless性能问题,其余的都能够基本正常. 依据实际须要要对已完毕的工作在AR9341平台上实现. 事实上也简单.基本功能 ...

  8. Oracle decode函数 除数为零

    decode (expression, search_1, result_1)如果 expression结果=search_1结果,则返回result_1,类似 if elsedecode (expr ...

  9. ORACLE触发器概述之【语句触发器】【weber出品】

    一.触发器概述 与表,视图,模式,或者数据库相关的PL/SQL过程,当触发条件被触发时,自动执行 分类: 1.语句触发器 2.行触发器 二.语句触发器 1. 什么是语句触发器 语句触发器,是指当执行D ...

  10. cell的各种使用和赋值 总结

    cell可以分为:自定义cell,系统的cell ,cell的自适应,.xib的cell //第一种cell:系统cell 在 UIViewController下创建UITableView //1.0 ...