Sliding Window Maximum
(http://leetcode.com/2011/01/sliding-window-maximum.html)
A long array A[] is given to you. There is a sliding window of size w which is moving from the very left of the array to the very right. You can only see the w numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and w is 3.
Window position Max
--------------- -----
[ -] -
[ - -]
[- - ]
- [- ]
- - [ ]
- - [ ]
Input: A long array A[], and a window width w
Ouput: An array B[], B[i] is the maximum value of from A[i] to A[i+w-1]
Requirement: Find a good optimal way to get B[i]
---
1. Brute force solution is O(nw)
2. Use heap, when window moves, delete the first one in the window, add the next one into the window. The run time complexity is O(n lg w).
3. Use double-ended queue. Code:
void maxSlidingWindow(int A[], int n, int w, int B[])
{
assert(A && n >= && w >= && w <= n && B); deque<int> Q;
for (int i = ; i < w; i++)
{
while (!Q.empty() && A[i] >= A[Q.back()])
Q.pop_back();
Q.push_back(i);
}
for (int i = w; i < n; i++)
{
B[i-w] = A[Q.front()];
while (!Q.empty() && A[i] >= A[Q.back()])
Q.pop_back();
while (!Q.empty() && Q.front() <= i-w)
Q.pop_front();
Q.push_back(i);
}
B[n-w] = A[Q.front()];
}
The above algorithm could be proven to have run time complexity of O(n). This is because each element in the list is being inserted and then removed at most once. Therefore, the total number of insert + delete operations in 2n.
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 解答
Question Given an array of n integer with duplicate number, and a moving window(size k), move the wi ...
- 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 ...
随机推荐
- 04737_C++程序设计_第9章_运算符重载及流类库
例9.1 完整实现str类的例子. #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> ...
- hdu5461 Largest Point(沈阳网赛)
Largest Point Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- LNMP : 502 Bad Gateway 解决小记,真正的原因
站点搬迁到新的server.原先一直都是LAMP.如今改为LNMP. 将重写文件 htaccess改成 nginx的 conf.放到了站点.可仅仅能打开首页,其它重写页面一打开都是不停的载入. 载入等 ...
- STL 源代码剖析 算法 stl_algo.h -- merge sort
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...
- MATLAB一句总结
MATLAB使用过程中的一些小总结: 1.sqrt函数的输入参数应为double类型: 2.im2bw把图像转换为二值图像: 3.double类型的图片必须转换为uint8类型后才能用imshow显示 ...
- 如何为你的美术妹子做Unity的小工具(三)
绘制脚本组件监视面板的内容 我们写了一个脚本Test using UnityEngine; using System.Collections; using System.Collections.Gen ...
- [ 转 ]jquery的ajax和getJson跨域获取json数据
目前浏览器端跨域访问常用的两种方法有两种: 1.通过jQuery的ajax进行跨域,这其实是采用的jsonp的方式来实现的. jsonp是英文json with padding的缩写.它允许在服务器端 ...
- Bash shell使用环境的终端的环境设置:stty
Bash shell使用环境的终端的环境设置:stty Bash shell使用环境的终端的环境设置:stty stty -a 将当前所有的stty参数列出来 intr:给正在运行的程序发送中断信号 ...
- 网络技术教程笔记(18)常见广域网技术——X.25与郑中基技术(←_←搜狗输入法你够了)
广域网与接入网技术 广域网与接入网技术 常见广域网技术--X.25 产生背景 尽管在当时,苹果二代计算机已经取得了很大的成功,但是PC和工作站却没有流行,也没有获得很多的网络支持,大多数人还是使用便宜 ...
- Material Design 开发利器:Android Design Support Library 介绍
转自:https://blog.leancloud.cn/3306/ Android 5.0 Lollipop 是迄今为止最重大的一次发布,很大程度上是因为 material design —— 这是 ...