The maximum sliding window: "prove safety offer" fifty-ninth title I

// interview questions 59 (a): the maximum value of the sliding window
 // Title: Given an array of size and sliding window, find the maximum value of all sliding window. For example,
 // if the input array {2, 3, 4, 2, 6, 2, 5, 1} 3, and the size of the sliding window, then the presence of a total of 6
 // sliding window, the maximum value thereof is {4, respectively, . 4,. 6,. 6,. 6,. 5}, 

#include <cstdio> 
#include <Vector> 
#include <the deque> the using namespace STD; 
Vector < int > maxInWindows ( const Vector < int > & NUM, unsigned int size) 
{ 
    Vector < int > maxInWindows;   // sliding window maximum value IF (num.size ()> = size && size> = . 1

 


    ) 
    { 
        The deque < int > index; // store the current window the maximum value 
        for (unsigned int I = 0 ; I <size; I ++)   // find the first maximum value of the window 
        {
             the while (index.empty (! ) && NUM [I]> NUM [index.back ()])   // if the current value is greater than the value of the last one in the queue, then eject 
                index.pop_back (); 
            
            index.push_back (I);   // Loading current value 
        } 

        for (unsigned int I = size; I <num.size (); I ++)   // iterate 
        { 
            maxInWindows.push_back (NUM [index.front ()]); //Maximum load on the window 

            the while (! Index.empty () && NUM [I]> = NUM [index.back ()])   // if the current value is greater than the value of the last one in the queue, then eject 
                index .pop_back (); 

            the while (index.empty () && I> = + index.front size ()!)   // if the current window has exceeded the maximum value, it will eject 
                index.pop_front (); 

            index.push_back (I );   // load current value 
        } 
        maxInWindows.push_back (NUM [index.front ()]); // Loading the maximum value of the window 
    }
     return maxInWindows; 
}
// ====================测试代码====================
void Test(const char* testName, const vector<int>& num, unsigned int size, const vector<int>& expected)
{
    if (testName != nullptr)
        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 != *iterExpected)
            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 + the sizeof (expected) / the sizeof ( int )); 

    unsigned int size = . 3 ; 

    the Test ( " Test2 " , vecNumbers, size, vecExpected) ; 
} 

// input array monotonically increasing 
void the 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); 
} 

// input array monotonically decreasing 
void Test4 () 
{ 
    int NUM [] = { 16 , 14 , 12 is , 10 , . 8 , . 6 , . 4 }; 
    Vector < int > vecNumbers (NUM, NUM + the sizeof (NUM) / the sizeof ( int )); 

    int expected [] = { 16 , 14 , 12 is }; 
    Vector < int > vecExpected (expected, + expected the sizeof(expected) / the sizeof ( int )); 

    unsigned int size = . 5 ; 

    the Test ( " Test4 " , vecNumbers, size, vecExpected); 
} 

// sliding window size is. 1 
void Test5 should be conducted () 
{ 
    int NUM [] = { 10 , 14 , 12 is , . 11 }; 
    Vector < int > vecNumbers (NUM, NUM + the sizeof (NUM) / the sizeof ( int )); 

    int expected [] = { 10 , 14, 12 is , . 11 }; 
    Vector < int > vecExpected (expected, + expected the sizeof (expected) / the sizeof ( int )); 

    unsigned int size = . 1 ; 

    the Test ( " Test5 should be conducted " , vecNumbers, size, vecExpected); 
} 

// slide window size is equal to the length of the array 
void Test6 () 
{ 
    int NUM [] = { 10 , 14 , 12 is , . 11 }; 
    Vector < int > vecNumbers (NUM, NUM +the sizeof (NUM) / the sizeof ( int )); 

    int expected [] = { 14 }; 
    Vector < int > vecExpected (expected, expected + the sizeof (expected) / the sizeof ( int )); 

    unsigned int size = . 4 ; 

    the Test ( " test6 " , vecNumbers, size, vecExpected); 
} 

// sliding window size is 0 
void TEST7 () 
{ 
    int NUM [] = { 10 , 14 , 12 is , . 11}; 
    Vector < int > vecNumbers (NUM, NUM + the sizeof (NUM) / the sizeof ( int )); 

    Vector < int > vecExpected; 

    unsigned int size = 0 ; 

    the Test ( " TEST7 " , vecNumbers, size, vecExpected); 
} 

/ / sliding window size greater than the length of the input array 
void test8 () 
{ 
    int NUM [] = { 10 , 14 , 12 is , . 11 }; 
    Vector < int> vecNumbers(num, num + sizeof(num) / sizeof(int));

    vector<int> vecExpected;

    unsigned int size = 5;

    Test("Test8", vecNumbers, size, vecExpected);
}

// 输入数组为空
void Test9()
{
    vector<int> vecNumbers;
    vector<int> vecExpected;

    unsigned int size = 5;

    Test("Test9", vecNumbers, size, vecExpected);
}

int main(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();
    Test8();
    Test9();

    return 0;
}
Test code

Analysis: Example analysis.

 

Guess you like

Origin www.cnblogs.com/ZSY-blog/p/12663917.html