Analysis of "original" Sliding window Maximum Solution

Source: Internet
Author: User

This problem is a problem on the Lintcode, of course leetcode on the same.

The subject needs to find an O (N) complexity algorithm.

The idea of disintegration is more characteristic, so it is easy to think about the problem solving method of reference minimum stack .

But minimum stack maintenance minimum value is very intuitive, this question is the queue, with what data structure good? Perhaps it would be a little enlightening to read the violent solution.

But the idea is the same, the biggest to be in front (directly get the result), the small value is left behind (to prevent the loss of data after the traversal). and a value out of the window when you need to determine whether to modify the top-ranked values.

One. Violent Solutions

Of course, the brute force solution is the complexity of O (NK), the general code is as follows: (write a bit complicated, mainly at that time is to build up the tail and the change of the queue through the vector, found in the STL actually gave up)

vector<int> Maxslidingwindow (vector<int> &nums,intk) {//Write your code here        if(Nums.empty ())returnvector<int>(); Vector<int>Maxvec; intMAXV =int_min; Vector<int>Res;  for(inti =0; I < K && I < nums.size (); i++) {            if(Maxv <nums[i]) MAXV=Nums[i];        Maxvec.push_back (Nums[i]);        } res.push_back (MAXV);  for(inti = k; I < nums.size (); i++) {maxvec.push_back (nums[i]);            Maxvec.erase (Maxvec.begin ()); if(Nums[i-k] = =MAXV) {MAXV=int_min;  for(intj =0; J < Maxvec.size (); J + +)                    if(Maxv <Maxvec[j]) MAXV=Maxvec[j]; }            if(Nums[i] >maxv) MAXV=Nums[i];        Res.push_back (MAXV); }                returnRes; }

Two. Big Top Pile

In fact, the idea is to get the maximum element by controlling O (1) in the construction of the heap, and the complexity of O (N * logk)

Three. Bidirectional queue

Finally to it, in fact, to himself is to remind yourself of the existence of STL deque. is as easy to ignore as priority_queue.

The idea is to maintain a non-sequential two-way queue with a maximum value at the head of the team (get results directly). The queue size is at most the window size and is controlled by the value out window.

The worst case time complexity is 2N, so O (N) complexity.

vector<int> Maxslidingwindow (vector<int> &nums,intk) {//Write your code heredeque<int>My_deq; Vector<int>Res;  for(inti =0; I < nums.size (); i++) {            if(I-k >=0) {//out-of-queue kick-head node judgmentif(!my_deq.empty () && nums[i-k] = =My_deq.front ()) My_deq.pop_front (); }
Into the queue kick value judgment
while(!my_deq.empty () && nums[i] >my_deq.back ()) My_deq.pop_back (); My_deq.push_back (Nums[i]); if(i +1-K >=0)//window size satisfies the judgment Res.push_back (My_deq.front ()); } if(k >nums.size ()) Res.push_back (My_deq.front ()); returnRes; }

Reprint please specify the source ~ http://www.cnblogs.com/xiaoboCSer/p/4895191.html

Analysis of "original" Sliding window Maximum Solution

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.