// 面试题59(一):滑动窗口的最大值 // 题目:给定一个数组和滑动窗口的大小,请找出所有滑动窗口里的最大值。例如, // 如果输入数组{2, 3, 4, 2, 6, 2, 5, 1}及滑动窗口的大小3,那么一共存在6个 // 滑动窗口,它们的最大值分别为{4, 4, 6, 6, 6, 5}, #include <iostream> #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()]);//先把队列头放入maxInWindows 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()]);//最后那个值没在循环中插入maxInWindows,要额外写一句 } 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(); system("pause"); return 0; }