<?php
$arr = [1,4,5,8, 100, 20, 30 , -1, 9, 6, 70, 35, 48, 90];
function maxSlidingWindow($arr, $k){
if(!$arr) return [];
$window = []; $res = [];
foreach($arr as $index => $val){
//由于窗口的移动,window内的元素下标已不在窗口的范围,则移出去
if($index>=$k && $window[0]<=$index-$k)
array_shift($window);
//如果window的最后一个下标所对应的元素没有当前元素大,则移出去
while($window && $arr[$window[count($window)-1]] <= $val)
array_pop($window);
//将当前下标推入window队列
array_push($window, $index);
//如果窗口已经满了,则提出最大值
if($index >= $k-1)
$res[] = $arr[$window[0]];
}
return $res;
}
print_r(maxSlidingWindow($arr, 3));