Find maximum of sliding window in O(n) time

by podlipensky

JavaScript

var a = [3,10,1,3,4,7,8,5,5,10,7],
    len = a.length,
    m = 4,
    v,
    d = [], //dequeue
    res = [];

for(var i = 0; i < len; i++){
    v = a[i];
    while(d.length && a[d[d.length - 1]] < v){
       d.pop();                
    }
    d.push(i);
    if((i-d[0]) > m-1){
       d.shift();            
    }
    res.push(a[d[0]]);
}
console.log(res);