79. 滑动窗口的最大值
2019-11-26 本文已影响0人
蜜糖_7474
题目地址:https://www.acwing.com/problem/content/75/
AC代码
class Solution {
public:
vector<int> maxInWindows(vector<int> &v, int k) {
if(v.empty()) return {};
vector<int> res;
deque<int> dq;//存放下标
for(int i=0;i<v.size();++i){
while(!dq.empty() && v[i]>v[dq.back()]) dq.pop_back();
dq.push_back(i);
if(i>=k-1) res.push_back(v[dq.front()]);
if(i-dq.front()==k-1) dq.pop_front();
}
return res;
}
};
总结
剑指offer题,貌似是最简单的单调队列应用
答案参考:https://blog.csdn.net/qq_37746778/article/details/88060936