Maximum of sliding window __ maximum of sliding window

Source: Internet
Author: User
Tags array length int size
question:

Given an array and the size of the sliding window, find the maximum number of values in all sliding windows.

For example, if the input array {2,3,4,2,6,2,5,1} and sliding window size 3, there are altogether 6 sliding windows, their maximum value is {4,4,6,6,6,5};

The sliding window for 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]}

(1) Violence law

Iterate through the array, starting at the w-1 bit of the array, moving one bit at a time, and calculating the maximum value of the window W.

Complexity of Time:

Calculate the maximum window value O (w), move n-w+1 times, time complexity of O (NW)

(2) Max Heap method

Build a maximum heap of the W size of a window, each time the maximum value of the window is fetched from the heap, and as the window slides to the right, a heap top element that does not belong to the window needs to be removed.

Complexity of Time:

Normally, inserting data into the heap is O (LGW), or O (LGN) If the array is ordered, because no elements in the slide are removed from the heap, sliding n-w+1 times, and the complexity is O (NLGN).

(3) Building double queues

The maximum heap solution holds redundant elements in the heap, such as the original heap element is [10 5 3], and the new element is 11, then [11 5 3] is saved in the heap. In fact, you can empty the entire queue at this time, and then add 11 to the queue, that is, only maintain [11] in the queue. Use bidirectional queues to meet the requirements, the maximum sliding window is always kept in the queue header, the data in the queue is always from large to small arrangement. When a value larger than the current sliding window is encountered, the queue is emptied and the new maximum value is inserted into the queue. If the value encountered is smaller than the current maximum value, it is inserted directly into the tail of the queue. Each time you move, you need to determine whether the current maximum is in the valid range, and if not, you need to remove it from the queue. Because each element is up and out each time, the algorithm has an O (N) complexity.

The detailed implementation code is as follows:

Maximum value in sliding window vector<int> maxinwindows (const vector<int>& num, unsigned int size) {vector<int> ma
    Xinwindows;

        if (num.size () >= size && size >= 1) {deque<int> index; for (unsigned int i = 0; i < size; ++i) {while (!index.empty () && num[i] >= Num[index.bac

            K ()]) index.pop_back ();
        Index.push_back (i); for (unsigned int i = size; I < num.size (); ++i) {Maxinwindows.push_back (Num[index.front

            ()]);
            while (!index.empty () && num[i] >= num[index.back ())) Index.pop_back ();

            if (!index.empty () && Index.front () <= (int) (i-size)) Index.pop_front ();
        Index.push_back (i);
    } maxinwindows.push_back (Num[index.front ()));
return maxinwindows; }//==================== test Code ==================== void Test (char* testname, Const vector<int>& num, unsigned int size, const vector<int>& expected) {if (testname!= NULL)

    printf ("%s begins:", testname);

    vector<int> result = maxinwindows (num, size);
    Vector<int>::const_iterator Iterresult = Result.begin ();
    Vector<int>::const_iterator iterexpected = Expected.begin (); while (Iterresult < Result.end () && iterexpected < Expected.end ()) {if (*iterresult!= *iterexpec

        Ted) break;
        ++iterresult;
    ++iterexpected;
    } if (Iterresult = = Result.end () && iterexpected = = Expected.end ()) printf ("passed.\n");
else printf ("failed.\n");
    } void Test1 () {int num[] = {2, 3, 4, 2, 6, 2, 5, 1};

    vector<int> vecnumbers (num, num + sizeof (num)/sizeof (int));
    int expected[] = {4, 4, 6, 6, 6, 5};

    Vector<int> vecexpected (expected, expected + sizeof (expected)/sizeof (int));

    unsigned int size = 3;Test ("Test1", vecnumbers, size, vecexpected);
    } void Test2 () {int num[] = {1, 3,-1,-3, 5, 3, 6, 7};

    vector<int> vecnumbers (num, num + sizeof (num)/sizeof (int));
    int expected[] = {3, 3, 5, 5, 6, 7};

    Vector<int> vecexpected (expected, expected + sizeof (expected)/sizeof (int));

    unsigned int size = 3;
Test ("Test2", vecnumbers, size, vecexpected);
    }//increasingly sorted void Test3 () {int num[] = {1, 3, 5, 7, 9, 11, 13, 15};

    vector<int> vecnumbers (num, num + sizeof (num)/sizeof (int));
    int expected[] = {7, 9, 11, 13, 15};

    Vector<int> vecexpected (expected, expected + sizeof (expected)/sizeof (int));

    unsigned int size = 4;
Test ("Test3", vecnumbers, size, vecexpected);
    }//decreasingly sorted void Test4 () {int num[] = {16, 14, 12, 10, 8, 6, 4};

    vector<int> vecnumbers (num, num + sizeof (num)/sizeof (int));
    int expected[] = {16, 14, 12}; Vector<int> vecexpected (expected, expected + sizeof (expected)/sizeof (int));

    unsigned int size = 5;
Test ("Test4", vecnumbers, size, vecexpected);
    /size of sliding windows is 1 void Test5 () {int num[] = {10, 14, 12, 11};

    vector<int> vecnumbers (num, num + sizeof (num)/sizeof (int));
    int expected[] = {10, 14, 12, 11};

    Vector<int> vecexpected (expected, expected + sizeof (expected)/sizeof (int));

    unsigned int size = 1;
Test ("Test5", vecnumbers, size, vecexpected);
    }//size of sliding windows is same as the array length void Test6 () {int num[] = {10, 14, 12, 11};

    vector<int> vecnumbers (num, num + sizeof (num)/sizeof (int));
    int expected[] = {14};

    Vector<int> vecexpected (expected, expected + sizeof (expected)/sizeof (int));

    unsigned int size = 4;
Test ("Test6", vecnumbers, size, vecexpected);
    /size of sliding windows is 0 void Test7 () {int num[] = {10, 14, 12, 11}; vector<int> vecnumbers (num, num + sizeof (num)/sizeof(int));

    Vector<int> vecexpected;

    unsigned int size = 0;
Test ("Test7", vecnumbers, size, vecexpected);
    /size of sliding windows is greater than the array length void Test8 () {int num[] = {10, 14, 12, 11};

    vector<int> vecnumbers (num, num + sizeof (num)/sizeof (int));

    Vector<int> vecexpected;

    unsigned int size = 5;
Test ("Test8", vecnumbers, size, vecexpected);
    int main (int argc, char* argv[]) {Test1 ();
    Test2 ();
    Test3 ();
    Test4 ();
    Test5 ();
    Test6 ();
    Test7 ();

	Test8 ();
return 0;

 }




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.