天,这题我已经没有底气高呼“水”了。。。

题目的地址:

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 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?

题目解析:

关键是线性时间。开头我试图通过动态规划定义子问题来解决,然而这并没有什么卵用,怀疑自己已经患上了一定程度的动态规划病,需要重读CLRS回炉一番

其实有第二个关键点。均摊复杂度和严格复杂度,对于“线性时间内解决”这个要求而言,并没有什么不同的地方。

第三个关键点,就是这是一个动态维护的队列,你要在不重新遍历队中元素的情况下维护一个最值。

不知道各位是否做过O(1)时间内维护一个栈中最值的问题,如果没有,可以看看这篇老文:

【原创】leetCodeOj --- Min Stack 解题报告

那位说了,刚才还讲本质上是动态队列,现在你拿栈出来坑蒙拐骗,放学别走

别急别急,不是还能用栈模拟队列吗?

两个栈,队列的push操作,就把元素压到栈1。队列的pop操作,首先检查栈2是否非空,若栈2有元素,直接pop。若栈2无元素,则把当前栈1中全部的元素压进栈2。

这样,我们能够维护一个栈中的最值,就能维护一个队列中的最值。

因为当前队列中的全部元素都分布在这两个栈中,因此,这两个栈的最值再比一轮,就是最后的最值。

又有人问了,pop一次,栈2有东西还好,若没有,就得捣腾半天,把栈1的元素挨个压进栈2,这算哪门子线性时间?

还真是线性时间,别忘了均摊复杂度

每个元素,进队被压进一次栈1,出队时被压进栈2一次,算上弹出操作2次,一共只有4次操作。

一共n个元素

那就是4n个操作

不就是线性吗?

关键在于,一次实际的操作可能只有弹出一次,而压栈的操作被集成在某次弹出时集中执行了。

具体代码:

public class Solution {

    public int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length == 0) {
return nums;
}
int[] res = new int[nums.length - k + 1];
MinQueue queue = new MinQueue();
for (int i = 0; i < k; i ++) {
queue.push(nums[i]);
}
res[0] = queue.getMax();
int index = 1;
for (int i = k; i < nums.length; i ++) {
queue.pop();
queue.push(nums[i]);
res[index ++] = queue.getMax();
}
return res;
} class MinQueue { LinkedList<Integer> stack1 = new LinkedList<Integer>();
LinkedList<Integer> stack2 = new LinkedList<Integer>();
LinkedList<Integer> maxOne = new LinkedList<Integer>();
LinkedList<Integer> maxTwo = new LinkedList<Integer>(); public void push(Integer item) {
pushOne(item);
} public Integer pop() {
Integer res = null;
if (stack2.size() == 0) {
while (stack1.size() != 0) {
pushTwo(popOne());
}
}
res = stack2.pop();
maxTwo.pop();
return res;
} public Integer getMax() {
Integer one = maxOne.peek();
Integer two = maxTwo.peek();
if (one == null) {
return two;
} else if (two == null){
return one;
}
return one > two ? one : two;
} private Integer popOne() {
Integer res = null;
maxOne.pop();
res = stack1.pop();
return res;
} private void pushOne(Integer item) {
stack1.push(item);
if (stack1.size() == 1) {
maxOne.push(item);
} else {
Integer front = maxOne.peek();
if (front < item) {
maxOne.push(item);
} else {
maxOne.push(front);
}
}
} private void pushTwo(Integer item) {
stack2.push(item);
if (stack2.size() == 1) {
maxTwo.push(item);
} else {
Integer front = maxTwo.peek();
if (front < item) {
maxTwo.push(item);
} else {
maxTwo.push(front);
}
}
}
} }

【原创】leetCodeOj --- Sliding Window Maximum 解题报告的更多相关文章

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

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

  2. 【原创】Sliding Window Maximum 解法分析

    这道题是lintcode上的一道题,当然leetcode上同样有. 本题需要寻找O(N)复杂度的算法. 解体思路比较有特点,所以容易想到参考 最小栈 的解题办法. 但是最小栈用栈维护最小值很直观,这道 ...

  3. leetcode面试准备:Sliding Window Maximum

    leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...

  4. 【LeetCode】239. Sliding Window Maximum

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

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

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

  6. [LeetCode] 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. Sliding Window Maximum 解答

    Question Given an array of n integer with duplicate number, and a moving window(size k), move the wi ...

  8. Sliding Window Maximum

    (http://leetcode.com/2011/01/sliding-window-maximum.html) A long array A[] is given to you. There is ...

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

随机推荐

  1. Android - 隐藏最顶端的通知条(Top Notification Bar)

    隐藏最顶端的通知条(Top Notification Bar/ActionBar) 本文地址: http://blog.csdn.net/caroline_wendy Android中, 视频播放等功 ...

  2. Oracle 10g AND Oracle 11g手工建库案例--Oracle 11g

    Oracle 10g AND Oracle 11g手工建库案例--Oracle 11g 系统环境: 操作系统: RedHat EL6 Oracle:  Oracle 10g and Oracle 11 ...

  3. Mac必备软件推荐

    阅读原文http://littlewhite.us/archives/245 随着IOS的流行.Mac电脑也越来越多的进入人们的视野,和iPhone系列一样,苹果的Mac产品线也是软硬件完美结合.有着 ...

  4. ruby语言仅仅是昙花一现

    Ruby语言本身存在非常久了,在国内一直没火过.非常多人仅仅是知道有这样的语言,会的人少之又少.不论什么一种语言坚持十来年的发展,变得越来越好,一定有它不平常的地方.不能任意的去比較语言本身的好与坏. ...

  5. 最短路知识点总结(Dijkstra,Floyd,SPFA,Bellman-Ford)

    Dijkstra算法: 解决的问题: 带权重的有向图上单源最短路径问题.且权重都为非负值.如果采用的实现方法合适,Dijkstra运行时间要低于Bellman-Ford算法. 思路: 如果存在一条从i ...

  6. java学习笔记11--Annotation

    java学习笔记11--Annotation Annotation:在JDK1.5之后增加的一个新特性,这种特性被称为元数据特性,在JDK1.5之后称为注释,即:使用注释的方式加入一些程序的信息. j ...

  7. Linux实现字符设备驱动的基础步骤

    Linux应用层想要操作kernel层的API,比方想操作相关GPIO或寄存器,能够通过写一个字符设备驱动来实现. 1.先在rootfs中的 /dev/ 下生成一个字符设备.注意主设备号 和 从设备号 ...

  8. 使用KnockoutJs+Bootstrap实现分页

    [后端人员耍前端系列]KnockoutJs篇:使用KnockoutJs+Bootstrap实现分页   一.引言 由于最近公司的系统需要改版,改版的新系统我打算使用KnockoutJs来制作Web前端 ...

  9. ffmpeg 频中分离 video audio 截取片断

    1.获取视频的信息    ffmpeg -i video.avi 2,将图片序列分解合成视频    ffmpeg -i src.mpg image%d.jpg ffmpeg -f image2 -i ...

  10. sql使用存储过程和交易

    在过去的一年.学习数据库的时候学校有存储过程.永远只是知道一些理论,我不知道怎么用.时隔一年,最终找到怎样使用存储过程了. 在机房收费系统中.有些操作.须要多次运行sql语句,多次运行完毕才算是完毕这 ...