class Solution {
public:
vector<int> res;
deque<int> q;
vector<int> maxInWindows(vector<int>& nums, int k) {
for (int i = 0; i < nums.size(); i ++ )
{
//先弹出左端点,保证队列里所有元素都是有效的
if(q.size()&&i-q.front()==k) q.pop_front();//队列里只存储可能的答案,不能直接用size与k比较
//加入右端点
while(q.size()&&nums[q.back()]<nums[i]) q.pop_back();
q.push_back(i);
//更新答案
if(i>=k-1) res.push_back(nums[q.front()]);
}
return res;
}
};
标签:窗口,nums,res,最大值,back,vector,front,滑动,size
From: https://www.cnblogs.com/tangxibomb/p/17397302.html