Offer- prove safety questions 59_1- maximum surface sliding window - an array

/ * 
Title: 
	link: https: //www.nowcoder.com/questionTerminal/1624bc35a45c42c0bc17d17fa0cba788 
	Source: cattle off network given the size of the array and a sliding window, sliding window to find all the values of the maximum. For example, if the input array size and {2,3,4,2,6,2,5,1} 3 sliding window, 
	then the presence of a total of six sliding window, their maximum values is {4,4,6, 6,6,5}; it has the following six array {2,3,4,2,6,2,5,1} for the sliding window:       
	{[2,3,4], 2,6,2,5 , 1}, {2, [3,4,2], 6,2,5,1}, {2,3, [4,2,6], 2, 5}, {2,3,4 , [2,6,2], 5,1}, {2,3,4,2, [6,2,5], 1}, {2,3,4,2,6, [2,5, 1]}. 
* / 
/ * 
Ideas: 
	Note unsigned subtraction 
* / 
#include <the iostream> 
#include <the cstdlib> 
#include <Stack> 
#include <String> 
#include <Vector> 
#include <the deque> 

the using namespace STD; 

Vector <int> maxInWindows (const vector <int> & num,
    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]<<" ";
    }



}

   

Guess you like

Origin www.cnblogs.com/buaaZhhx/p/12114982.html