Question

Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration from the start of the array, find the maximum number inside the window at each moving.

Example

For array [1, 2, 7, 7, 8], moving window size k = 3. return [7, 7, 8]

At first the window is at the start of the array like this

[|1, 2, 7| ,7, 8] , return the maximum 7;

then the window move one step forward.

[1, |2, 7 ,7|, 8], return the maximum 7;

then the window move one step forward again.

[1, 2, |7, 7, 8|], return the maximum 8;

Challenge

o(n) time and O(k) memory

Solution

Key to the solution is to maintain a deque (size <= k) whose elements are always in descending order. Then, the first element is what we want.

Every time we want to add a new element, we need to check:

1. whether it is bigger than previous elements in deque.

If yes, we remove elements in deque which are smaller than current element.

2. whether the first element in deque is out of current sliding window.

If yes, we remove first element.

 public class Solution {

     public ArrayList<Integer> maxSlidingWindow(int[] nums, int k) {
ArrayList<Integer> result = new ArrayList<Integer>();
Deque<Integer> deque = new LinkedList<Integer>();
int i = 0;
for (int current : nums) {
i++;
// Ensure current deque is in decending order
while (!deque.isEmpty() && deque.peekLast() < current)
deque.pollLast();
deque.addLast(current);
if (i > k && deque.peekFirst() == nums[i - k - 1])
deque.pollFirst();
if (i >= k)
result.add(deque.peekFirst());
}
return result;
}
}

Sliding Window Maximum 解答的更多相关文章

  1. leetcode面试准备:Sliding Window Maximum

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

  2. 【LeetCode】239. Sliding Window Maximum

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

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

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

  4. Sliding Window Maximum

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

  5. Sliding Window Maximum LT239

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

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

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

  8. Leetcode: sliding window maximum

    August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...

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

随机推荐

  1. Android数据库信息显示在listview上

    Key Points: 1.使用SimpleCursorAdapter将Android数据库信息显示在listview上 adapter = new SimpleCursorAdapter(this, ...

  2. pythonchallenge学到的python内置函数整理

    第0关: 计算x的n次方: x**n 第一关: maketrans(from,to):建立一个翻译规则,将from翻译成to的翻译规则,因为要从from翻译成to,所以俩个参数的长度必须一致 tran ...

  3. HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...

  4. mysql 存储过程:提供查询语句并返回查询执行影响的行数

    mysql 存储过程:提供查询语句并返回查询执行影响的行数DELIMITER $$ DROP PROCEDURE IF EXISTS `p_get_select_row_number`$$ CREAT ...

  5. Java 反射 方法调用

    在使用Java 反射时,对方法的调用,可能碰到最多的问题是,方法的变量如何使用.其实,调用方法的变量全部在参数数组里,不管有多少个参数,你都要把它放在参数数组里,如果是单个非数组参数,则可不使用参数数 ...

  6. JMeter数据库性能测试

    要测试一个服务器的性能,客户要求向数据库内 1000/s(每插入一千条数据)的处理能力 前提条件:一个数据库:test   数据库下面有一张表:user   表中有两个字段:username.pass ...

  7. c++11 线程:让你的多线程任务更轻松

      介绍 本文旨在帮助有经验的Win32程序员来了解c++ 11线程库及同步对象 和 Win32线程及同步对象之间的区别和相似之处. 在Win32中,所有的同步对象句柄(HANDLE)是全局句柄.它们 ...

  8. jquery压缩图片插件

    imageCompress 只有图片压缩功能,比较简单jquery.imageCompress.js 使用说明: el:为上传框 quality:压缩图片质量,单位为% onloadStart:读取图 ...

  9. Android企业级程序完全退出的解决方案

    一.问题描述 在平常开发的过程中可以发现,很多开发者对于程序的退出都没有去认真的解决.一般要么是一个简单的finish(只是退出当前的activity),要么是其他的方法,比如: 1.第一种方法:首先 ...

  10. Android调用系统邮件类应用的正确实现方法

    Android应用开发中,很多情况下免不了要调用手机上的邮件类应用,实现邮件发送的功能,这一般是通过调用系统已有的Intent来实现的.看到网上很多邮件发送都是调用action为android.con ...