/*
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。
例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,
他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个:
{[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1},
{2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。
*/
import java.util.*;
public class Class67 {
public ArrayList<Integer> maxInWindows(int [] num, int size){
ArrayList<Integer> result = new ArrayList<Integer>();
if(num == null || num.length <= 0 || size <= 0){
return result;
}
int[] subNum = new int[size];
int operationNum = num.length - size + 1;
int index = 0;
int index1 = 0;
int index2 = size - 1;
while(operationNum > 0){
for(int i = 0; i < num.length; i++){
if(index1 <= i && index2 >= i && index2 < num.length){
subNum[index] = num[i];
index++;
}
}
result.add(calAverage(subNum));
index = 0;
index1++;
index2++;
operationNum--;
}
return result;
}
public int calAverage(int[] subNum){
int windowLength = subNum.length;
int maxMath = subNum[0];
for(int i = 1; i < windowLength; i++){
if(maxMath < subNum[i]){
int temp = maxMath;
maxMath = subNum[i];
subNum[i] = temp;
}
}
return maxMath;
}
public void test(){
int[] num = {2,3,4,2,6,2,5,1};
int size = 3;
System.out.println(maxInWindows(num, size));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Class67 c = new Class67();
c.test();
}
}