最大窗口
代码实现如下:
package com.lilydedbb;
import java.util.LinkedList;
/**
* Created by dbb on 2016/12/24.
*/
public class GetMaxWindow {
public static int[] getMaxWindow(int[] arr, int w){
if (arr == null || w < 1 || arr.length < w)
return null;
LinkedList<Integer> qmax = new LinkedList<Integer>();
int[] res = new int[arr.length - w + 1];
int index = 0;
for(int i = 0; i < arr.length; i++){
while(!qmax.isEmpty() && arr[qmax.peekLast()] <= arr[i]){
qmax.pollLast();
}
qmax.addLast(i);
if(qmax.peekFirst() == i - w) {
qmax.pollFirst();
}
if(i >= w - 1){
res[index++] = arr[qmax.peekFirst()];
}
}
return res;
}
}