79. 滑动窗口的最大值

题目地址:https://www.acwing.com/problem/content/75/

AC代码

class Solution {
public:
    vector maxInWindows(vector &v, int k) {
        if(v.empty()) return {};
        vector res;
        deque dq;//存放下标
        for(int i=0;iv[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

你可能感兴趣的:(79. 滑动窗口的最大值)