1 class Solution { 2 public: 3 vector<int> maxInWindows(const vector<int>& num, unsigned int size) 4 { 5 vector<int> res; 6 if(num.empty() || size<=0) 7 return res; 8 int i=0; 9 for(; i<size; ++i) 10 { 11 if(index.empty()) 12 { 13 index.push_back(i); 14 } 15 else 16 { 17 if(num[i] >= num[index.back()]) 18 index.pop_back(); 19 index.push_back(i); 20 21 } 22 } 23 24 for(; i<=num.size(); ++i) 25 { 26 res.push_back(num[index.front()]); 27 while(!index.empty() && num[i]>=num[index.back()]) 28 index.pop_back(); 29 index.push_back(i); 30 while(!index.empty() && index.front()<=(i-size)) 31 index.pop_front(); 32 33 } 34 return res; 35 } 36 private: 37 deque<int> index; 38 };