"Sword Point offer": [65] maximum value of sliding window

Source: Internet
Author: User

title: Given an array and the size of the sliding window, find the maximum value in all the sliding windows.
For example, if the input array {2,3,4,2,6,2,5,1} and the size of the sliding window are 3, there are altogether 6 sliding windows with a maximum value of {4,4,6,6,6,5} respectively.

Sliding window This concept has been written in the network programming people should be not unfamiliar, mainly for the flow control. Using the size of the buffer data area of the receiver to control the sending speed, to avoid sending too fast, causing network congestion and other failure problems.
Scenario One: Brute force method, sequential block scan. For example, in the example above, we are constantly grouping and finding, 3 groups, which will eventually find its maximum value. But its time complexity is O (NK). n is the number of sliding windows, and K is the size of the sliding window.
Scenario Two: stack implementation. Sliding window We know that it is the FIFO data processing order, which is obviously a queue. In [21] We know we can use the stack to achieve an O (1) time complexity to get the maximum value, in [6] We also talked about using two stacks to implement a queue, so we can use two stacks to implement the queue, but also can use O (1) time to get the maximum value in the stack. So the total time complexity is reduced to O (N).

Scenario Three: Double-ended queue implementation. because the steps implemented in scenario two are complex, we change the idea that in the process of obtaining the maximum value, we do not put each value in the queue, but only the data that is likely to be the maximum value in the queue (deque) of the two ends of the opening, the above input as an example, the solution process is as follows:


The specific implementation code is as follows:
#include <iostream> #include <vector> #include <deque>using namespace Std;int arr[8]={ 2,3,4,2,6,2,5,1};vector<int> Array (arr,arr+8);d eque<int> index;vector<int> MaxWindows;vector <int> getmaxinwindows (const vector<int> &data,int size) {if (Data.size () >=size && size>= 1) {for (int i=0;i<size;i++)//first three to queue and find the maximum value; {while (!index.empty () && data[i]>=data[index.back ()]) Index.pop_back (); Index.push_back (i);} for (int i=size;i<data.size (); i++) {Maxwindows.push_back (Data[index.front ())),//maximum value such as queue; while (!index.empty () && Data[i]>=data[index.back ()]) index.pop_back (); if (!index.empty () && Index.front () <= (int) ( i-size)) Index.pop_front ();//The maximum value has been slid out of the window; index.push_back (i);} Maxwindows.push_back (Data[index.front ()));//The last number must be the largest;}return maxwindows;} int main () {vector<int> result;result=getmaxinwindows (array,3); Vector<int>::iterator it;cout<< " The maximum value of the sliding window is: "; for (It=result.begin (); It!=result.end (); it++) cout<<*it<< ""; Cout<<endl;system ("pause"); return 0;} 

Operation Result:


"Sword Point offer": [65] maximum value of sliding window

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.