Sliding Window Maximum 解答
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.
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;
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 解答的更多相关文章
- leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
- Sliding Window Maximum
(http://leetcode.com/2011/01/sliding-window-maximum.html) A long array A[] is given to you. There is ...
- 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 ...
- LeetCode题解-----Sliding Window Maximum
题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...
- [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 ...
- Leetcode: sliding window maximum
August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...
- 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 ...
随机推荐
- hdu4623:crime 数学优化dp
鞍山热身赛的题,也是去年多校原题 题目大意: 求n个数的排列中满足相邻两个数互质的排列的数量并取模 当时的思路就是状压dp.. dp[i][state] state用二进制记录某个数是否被取走,i ...
- dictionary (key-value) (map容器)
#dictionary可以保存不同类型的值 menu = {'fish0':14.50} list = [] menu['fish1'] = 10.1 # Adding new key-value p ...
- 360网站卫士常用前端公共库CDN服务
360网站卫士常用前端公共库CDN服务 360网站卫士常用前端公共库CDN服务
- MYSQL中的语句
MYSQL中的语句 decimal(8,2):最多存10位数的数字,小数点后保存两位.如:999999.99
- php中strstr、strrchr、substr、stristr四个函数的区别总结
php中strstr.strrchr.substr.stristr四个函数的区别总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-09-22我要评论 这篇文章主要介绍了php ...
- js推断元素是否隐藏
if( document.getElementById("div").css("display")==='none') if( document.getEl ...
- StartService与BindService
效果图 MainActivity.java package com.wangzhen.servicedemo; import com.lidroid.xutils.ViewUtils; import ...
- java二维不定长数组测试
package foxe; import javax.swing.JEditorPane;import javax.swing.JFrame; /** * @author fooxe * * @see ...
- 45个非常有用的 Oracle 查询语句小结
45个非常有用的 Oracle 查询语句小结 这里我们介绍的是 40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有 ...
- 对plist文件的简单封装
常常会用到对plist文件的封装 +(NSArray *)LoadFriendsDataFromPlist:(NSString *)plistName{ NSString * filePath = [ ...