Maximum value of sliding window (STL application + Sword-point offer)

Source: Internet
Author: User

Maximum value of sliding window
    • Number of participants: 767 time limit: 1 seconds space limit: 32768K
    • By scale: 21.61%
    • Best record: 0 ms|8552k(from)
The title describes the size of the given array and the sliding window, and finds the maximum value of the values in all the sliding windows. For example, if you enter the array {2,3,4,2,6,2,5,1} and the size of the sliding window 3, there are 6 sliding windows, their maximum value is {4,4,6,6,6,5}, and the sliding window for the array {2,3,4,2,6,2,5,1} has the following 6: {[ 2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[ 2,5,1]}.Ideas:

With a double-ended queue, the first position of the queue holds the maximum value of the current window, when the window slides once
1. Determine if the current maximum value expires
2. The newly added value is compared from the tail of the team, throwing away all the values that are smaller than him.
monotone queue, O (n)


Links: Http://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788?rp=4&ru=/ta/coding-interviews&qru =/ta/coding-interviews/question-ranking



The/** topic describes the size of the given array and sliding window, and finds the maximum value of the values in all the sliding windows. For example, if you enter the array {2,3,4,2,6,2,5,1} and the size of the sliding window 3, there are 6 sliding windows, their maximum value is {4,4,6,6,6,5}, and the sliding window for the array {2,3,4,2,6,2,5,1} has the following 6: {[ 2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[ 2,5,1]}.                                                    With a double-ended queue, the first position of the queue holds the maximum value of the current window, and when the window slides once 1. Determines whether the current maximum value expires 2. The new added value is compared from the end of the team, throwing away all the values that are smaller than him. Monotone queue, O (n) */#include <cstdio> #include <vector> #include <deque> #include <iostream> #include <algorithm>using namespace Std;class Solution {public:vector<int> maxinwindows (const vector<int>& Amp        num, unsigned int size) {vector<int> ans;        if (num.size () <size | | num.size () ==0 | | size<=0) return ans;  deque<int> tmp;        Double-ended queue int len=size,mmax=-1,mj=-1;                for (int i=0;i<len;i++) {if (Num[i]>mmax) {mmax=num[i];            Mj=i; }} for (int i=mj;i<len;i++) Tmp.push_back (i); All subscripts after the maximum value in Len should be placed in the container. Instead of just//Tmp.push_back (MJ);//The subscript of the largest value in the previous Len is placed in Ans.push_back (Mmax);//Place the maximum value of the former Len in the for (int I=LEN;I&LT;NUM.S            Ize (); i++) {//Deque<int>::iterator iter=tmp.begin ();//while (Iter!=tmp.end ())//            {//cout<<*iter++<< "";//}cout<<endl;            int t=num[i];  int K=tmp.front ();            The maximum value of the subscript if (i-k >= len)//Remove the already outdated maximum value!!!                   {while (i-k >= len) {tmp.pop_front ();                    if (!tmp.empty ()) K=tmp.front ();               else {tmp.push_back (i); break;}   }} int J=tmp.back ();                The last value of the subscript//Add a new number, and overwrite the previous can overwrite the value of the smaller than their own if (I!=j && num[i]>=num[j]) { while (Num[i]>=num[j]) {tmp.pop_back ();                   if (!tmp.empty ()) J=tmp.back ();                else break;            } tmp.push_back (i);                        } else Tmp.push_back (i);            K=tmp.front ();        Ans.push_back (Num[k]);    } return ans;    }};int Main () {vector<int> arr; unsigned int len=3;    /* Arr.push_back (2);    Arr.push_back (3);    Arr.push_back (4);    Arr.push_back (4);    Arr.push_back (6);    Arr.push_back (2);    Arr.push_back (3);    Arr.push_back (2);    Arr.push_back (5);    Arr.push_back (1); */Arr.push_back (16);    Arr.push_back (14);    Arr.push_back (12);    Arr.push_back (10);    Arr.push_back (8);    Arr.push_back (6);    Arr.push_back (4);    Arr.push_back (2);/* Arr.push_back (2);    Arr.push_back (3);    Arr.push_back (4);    Arr.push_back (2);    Arr.push_back (6);    Arr.push_back (2);    Arr.push_back (5);    Arr.push_back (1); */solution so;    Arr=so.maxinwindows (Arr,len); Vector<int>::itErator Iter=arr.begin ();    while (Iter!=arr.end ()) {cout<<*iter++<< "";    }cout<<endl; return 0;}

These days of learning cars,,, topics and so on, for a long time did not do,, and immediately will be two, just drift.

Maximum value of sliding window (STL application + Sword-point offer)

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.