(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的更多相关文章

  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 解答

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

  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. ARM Cortex M3(V7-M架构)硬件启动程序 二

    解析 STM32 的启动过程 解析STM32的启动过程 当前的嵌入式应用程序开发过程里,并且C语言成为了绝大部分场合的最佳选择.如此一来main函数似乎成为了理所当然的起点——因为C程序往往从main ...

  2. 提高Delphi的编译速度(bpl和bcp)

    delphi的编译速度提高(一) 此博文为原创,转载请注明出处 作者 :二娃 此博文的内容我曾经回答群内和论坛内的网友提问时回答过,现在写第一部分,第二部分,我再给出一个终极的提高速度的方法 我用过d ...

  3. rsyslog 配置详解

    格式:: 日志设备(类型).(连接符号)日志级别 日志处理方式(action) 日志设备(可以理解为日志类型): ------------------------ auth –pam产生的日志 aut ...

  4. C语言入门(9)——局部变量与全局变量

    变量有效性的范围称变量的作用域.C语言中所有的量都有自己的作用域.变量说明的方式不同,其作用域也不同. C语言中的变量,按作用域范围可分为两种,即局部变量和全局变量.   局部变量 局部变量也称为内部 ...

  5. poj2163

    #include <stdio.h> #include <stdlib.h> int main() { ; ]; scanf("%d %d %d",& ...

  6. Linux 抓包

    tcpdump -i eth1 -s 0 -w eth1_2.log  tcp port 8893

  7. Hadoop学习之YARN框架

    转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/,非常感谢分享! 对于业界的大数据存储及分布式处理系统来说,H ...

  8. Oracle经典书籍推荐

    转自:http://www.cnblogs.com/fjfzhkb/archive/2007/12/05/983381.html 很多网友询问如何选择入门书籍,学Oracle有什么好书,这里给出一些常 ...

  9. websocket 通信协议

    //WEBSOKET java SERVICE http://my.oschina.net/u/590484/blog/71797 UPDATE:前些天有网友mail和我讨论websocket协议,当 ...

  10. python 时间字符串与日期转化

    python 时间字符串与日期转化 datetime.datetime.strptime(string, format) 根据指定的格式解析字符串为一个datetime类型.相当于datetime.d ...