/*
题目:
	链接:https://www.nowcoder.com/questionTerminal/1624bc35a45c42c0bc17d17fa0cba788
	来源:牛客网	给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,
	那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下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]}。
*/
/*
思路:
	注意unsigned的减法
*/
#include<iostream>
#include<cstdlib>
#include<stack>
#include<string>
#include<vector>
#include<deque>

using namespace std;

vector<int> maxInWindows(const vector<int>& num, unsigned int size)
{
    vector<int> res;
    int length = num.size();
    int end = length - size;
    if(end < 0 || size < 1) return res;
    deque<int> window;
    int start = 0;
    for(int i = 0; i < length; i++){
        if(!window.empty()){
            if(window.front() == i - size){
                window.pop_front();
            }
            while(!window.empty() && num[window.back()] <= num[i]){
                window.pop_back();
            }
        }
        window.push_back(i);
        if(i+2 > size){
          res.push_back(num[window.front()]);
        }

    }
    return res;


}


int main()
{
    vector<int> A = {10,14,12,11};
    vector<int> B = maxInWindows(A,1);
    for(int i = 0; i < B.size(); i++){
        cout<<B[i]<<" ";
    }



}

   

posted on 2019-12-29 14:36  笨宝宝  阅读(147)  评论(0编辑  收藏  举报