滑动窗口问题
-------2023.5.16-------
做力扣又一次忘记怎么做啦!hhh
算法:(最大值为例)
1. 说明:队列q存放数组的下标,q维护一系列单调递减的数(称左边队头右边队尾)。
2. 遍历数组,新元素t,如果队列为空或队列维护的数组区间长度超过k,则队头弹出;
3. 循环若队列空或t大于队尾,则弹出队尾。
4. 合适条件下,输出队头
code
class Solution {
public:
const int N = 1e5 + 10;
vector<int> maxSlidingWindow(vector<int>& a, int k) {
int hh, tt;
hh = 0, tt = -1;
int q[N];
vector<int> res;
int n = a.size();
for(int i = 0; i < n; ++i){
if(hh <= tt && i - k + 1 > q[hh]) ++hh;
while(hh <= tt && a[q[tt]] <= a[i]) --tt;
q[++tt] = i;
if(i >= k - 1) res.push_back(a[q[hh]]);
}
return res;
}
};
-------2023.4.3--------
反复隔几个月敲 滑动队列的模板,发现思路是有,代码不知道怎么敲,这里给出一个这个问题的复习指导
Most importantly,滑动窗口问题用双端队列,队列中记录的是数组的下标值
敲一下模板题
https://www.acwing.com/activity/content/code/content/6166965/ (模板)
练一下,差不多的一道题
https://www.acwing.com/problem/content/156/
就复习的差不多了。。。因为我没怎么刷这个类型,之后遇到了再总结吧