《剑指offer》第五十九题I:滑动窗口的最大值

Posted zsy-blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《剑指offer》第五十九题I:滑动窗口的最大值相关的知识,希望对你有一定的参考价值。

// 面试题59(一):滑动窗口的最大值
// 题目:给定一个数组和滑动窗口的大小,请找出所有滑动窗口里的最大值。例如,
// 如果输入数组{2, 3, 4, 2, 6, 2, 5, 1}及滑动窗口的大小3,那么一共存在6个
// 滑动窗口,它们的最大值分别为{4, 4, 6, 6, 6, 5},

#include <cstdio>
#include <vector>
#include <deque>

using namespace std;

vector<int> maxInWindows(const vector<int>& num, unsigned int size)
{
    vector<int> maxInWindows;  //滑动窗口最大值

    if (num.size() >= size && size >= 1)
    {
        deque<int> index; //存放当前窗口最大值
        for (unsigned int i = 0; i < size; ++i)  //找出第一个窗口的最大值
        {
            while (!index.empty() && num[i] >= num[index.back()])  //如果当前值大于队列中最后一位值, 则将其弹出
                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();

            while (!index.empty() && i >= size + index.front() )  //如果当前最大值已经超出窗口, 则将其弹出
                index.pop_front();

            index.push_back(i);  //载入当前值
        }
        maxInWindows.push_back(num[index.front()]); //载入上个窗口的最大值
    }
    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.
");
    else
        printf("FAILED.
");
}

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);
}

// 输入数组单调递增
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);
}

// 输入数组单调递减
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);
}

// 滑动窗口的大小为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);
}

// 滑动窗口的大小等于数组的长度
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);
}

// 滑动窗口的大小为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);
}

// 滑动窗口的大小大于输入数组的长度
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);
}

// 输入数组为空
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;
}
测试代码

分析:举例分析。

技术图片
class Solution {
public:
    vector<int> maxInWindows(const vector<int>& num, unsigned int size)
    {
        vector<int> maxInWindows;
        
        if (num.size() >= size && size >= 1)
        {
            deque<int> index;
            for (unsigned int i = 0; i < size; ++i)
            {
                while (!index.empty() && num[i] >= num[index.back()])
                    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();
                while (!index.empty() && i >= size + index.front())
                    index.pop_front();
                
                index.push_back(i);
            }
            maxInWindows.push_back(num[index.front()]);
        }
        return maxInWindows;
    }
};
牛客网提交代码

 

以上是关于《剑指offer》第五十九题I:滑动窗口的最大值的主要内容,如果未能解决你的问题,请参考以下文章

《剑指offer》第五十九题(队列的最大值)

《剑指offer》第五十九题II:队列的最大值

leecode第五十九题(螺旋矩阵 II)

第五十九题——[NPUCTF2020]ReadlezPHP

leetcode 简单 第五十九题 同构字符串

《剑指offer》第四十九题(丑数)