单调队列
代码
#include <bits/stdc++.h>
using namespace std;
class MonotonicQueue{
deque<int> data_;
public:
void push(int e){
while (!data_.empty() && e > data_.back()) data_.pop_back();
data_.push_back(e);
return ;
}
void pop(void) {return data_.pop_front();}
int max(void) const {return data_.front();}
};
int main(void){
MonotonicQueue q;
vector<int> nums{89,32,6,1,100,3901,56,78,2};
for (int i = 0;i < (int)nums.size();++i){
q.push(nums[i]);
cout << q.max() << endl;
}
return 0;
}
使用场景
leetcode : 239.滑动窗口最大值、1696. 跳跃游戏 VI