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. SRM 598 DIV1

    A 只有3种情况:200以上的单独装,3个100的装一起,某两个u,v装一起且u+v<=300, 所以做法是从大到小判断每个大小的最大能与它装一起的是谁,最后剩下100的特判. B 第一轮如果未 ...

  2. Entify Framewrok - 学习链接

    http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading- ...

  3. HTML5新增的一些属性和功能之一

    大致可以分为10个方面: HTML5表单元素和属性 表单2.0 视音频处理 canvas绘图 SVG绘图 地理定位 拖放技术 web work web storage web socket 一.新的i ...

  4. Android: 在WebView中获取网页源码

    1. 使能javascript: ? 1 webView.getSettings().setJavaScriptEnabled(true); 2. 编写本地接口 ? 1 2 3 4 5 final c ...

  5. ZOJ 3430 Detect the Virus 【AC自动机+解码】

    解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...

  6. jquery获取当前元素坐标

    1. jquery获取当前元素坐标 A) 获取对象

  7. (转)用来理解Java的8个图表

    很多时候,一张图比你说 1000 个字能更有效的说清楚一个问题.我们列举了 8 个关于 Java 语言的图表,或许可以让你对 Java 有着更深入的认识. 1.字符串不变性(String Immuta ...

  8. C#不用COM组件导出数据到Excel中

    <?xml version='1.0'?><?mso-application progid='Excel.Sheet'?><Workbook xmlns='urn:sch ...

  9. c#串口编程和单片机通信重大发现

    1.遇到问题时看看这里 //每次响应中断结束后清空缓存,防止在显示关闭时,打开后又一次性出现 serialPort1.DiscardInBuffer();

  10. jquery的uploadify上传jsp+servlet

    1.准备材料:下载jquery.uploadify上传js   注意:这个上传在firefox下会出现问题如果你在项目中加了拦截器,因为session会丢失,所以你可以传参的时候带上你所需要的条件,在 ...