"Deque" sliding window, double-ended queue solves array problem

Source: Internet
Author: User

C + + Handbook of Deque

Owning header file <deque>

Common operations:

Back () returns the trailing element;

Front () returns the head element;

Push_back () tail insertion element;

POP_BAKC () tail delete element;

Push_front () head insertion element;

Pop_front () head delete element;

Question 1: Find the maximum value of the sliding window ("The sword refers to the offer surface question 65")

Description: Given an array and the size of the sliding window, find the maximum value in all the sliding windows.

Example: The array {2, 3, 4, 2, 6, 2, 5} has a window size of 3, a total of 7-3+1=5 sliding windows, returns an array of the maximum values of these windows {4, 4, 6, 6, 6}

Method: Generate a double-ended queue deque, which stores the subscript that may be the maximum value of a sliding window in an array.

vector<int> Getmax (Constvector<int> &arr,intsize) {Vector<int>Res; if(arr.size () = =0|| Size <=0){        returnRes; } deque<int>index; inti =0;  while(I <arr.size ()) {         while(!index.empty () && arr[index.back ()] <=Arr[i])        {Index.pop_back ();        } index.push_back (i); if(!index.empty () && index.front () = = (I-size)) {Index.pop_front ();//does not belong to the current window        }        if(I >= Size-1) {Res.push_back (Arr[index.front ())); } I++; }    returnRes;}

Question 2: Given an array, find out the difference between the maximum and minimum values is less than the number of sub-arrays equal to the given number Num. (New Ket left Chengyun "Programmer interview Code Guide")

Example: Given array {5, 2, 6, 3, 4, 1},num = 3, the eligible sub-arrays are {5} {5}, 2} {2} {6} {6, 3} {6}, 3, 4} {3} {3, 4} {3, 4, 1} {4} {} 4

Method: Generate two double-ended queue Qmax, Qmin, the first-level while loop I, satisfies the condition of I as the starting element of the sub-array, the second-level while loop, satisfies the condition of j as the tail element of the sub-array. Each time the inner while loop ends, the result res + = J-i;

Qmax maintains the subscript of the maximum value of the window sub-array in the original array;

Qmin maintains the subscript of the window sub-array minimum value in the original array.

The window changes dynamically, and the elements of Qmax and qmin are also changing.

1 intGetnum (intArr[],intLenintnum) {2     if(arr = NULL | | Len <1|| Num <0)3         return 0;4     intres =0;5deque<int>Qmax;6deque<int>qmin;7     inti =0;8     intj =0;9      while(I <Len) {Ten          while(J <Len) { One              while(!qmax.empty () && arr[qmax.back ()] <=Arr[j]) { A Qmax.pop_back (); -             } - Qmax.push_back (j); the              while(!qmin.empty () && arr[qmin.back ()] >=Arr[j]) { - Qmin.pop_back (); -             } - Qmin.push_back (j); +             if(Arr[qmax.front ()]-Arr[qmin.front ()] >num) { -                  Break; +             } AJ + +The//j has been getting bigger, not back. at         } -         if(Qmax.front () = =i) { - Qmax.pop_front ();//does not belong to a window that starts with i+1, pops up -         } -         if(Qmin.front () = =i) { - Qmin.pop_front ();//does not belong to a window that starts with i+1, pops up in         } -Res + = J-i; toi++the//i has been getting bigger, not back. +     } -     returnRes; the}

"Deque" sliding window, double-ended queue solves array problem

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.