What is mono-queue
the window | minimum | maximum |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
deque<int> window;
for(auto i = 0; i < n; i++){
if(window.size()!= 0 && (i - window.front() == l)){
window.pop_front();
}
while(window.size()!=0 && window.back() < a[i]){
window.pop_back();
}
window.push_back(i);
if(i < l-1){
continue;
}
Remember in one important thing: either the mono-stack or the mono-queue stores the index!, which will help to locate the position in the original given array.