Title Description
Given an array and the size of the sliding window, sliding window to find the maximum value among all values. For example, if the input array size and {2,3,4,2,6,2,5,1} 3 sliding window, then the presence of a total of six sliding window, their maximum values is {4,4,6, 6,6,5}; it has the following six array {2,3,4,2,6,2,5,1} for the sliding window: {[2,3,4], 2,6,2,5 , 1}, {2, [3,4,2], 6,2,5,1}, {2,3, [4,2,6], 2, 5}, {2,3,4 , [2,6,2], 5,1}, {2,3,4,2, [6,2,5], 1}, {2,3,4,2,6, [2,5, 1]}.
Problem-solving ideas
Through the array, a queue is loaded with a digital window size, maximum number of queue go to
code show as below
public class MaxInWindows {
public ArrayList<Integer> maxInWindows(int [] num, int size)
{
if (size==0||num.length==0) {
return new ArrayList<Integer>();
}else {
ArrayList<Integer> list=new ArrayList<Integer>();
for (int i = 0; i < num.length-size+1; i++) {
Queue<Integer> queue=new LinkedList<Integer>();
for (int j = i; j < i+size; j++) {
queue.add(num[j]);
}
int max=Integer.MIN_VALUE;
for(Integer integer:queue) {
max=Math.max(max, integer);
}
list.add(max);
}
return list;
}
}
public static void main(String[] args) {
MaxInWindows maxInWindows=new MaxInWindows();
int [] num=new int[] {2,3,4,2,6,2,5,1};
ArrayList<Integer> list=maxInWindows.maxInWindows(num, 3);
for(Integer integer:list) {
System.out.println(integer);
}
}
}