栈和队列之生成窗口最大值数组

103 阅读1分钟
package com.chenyu.zuo.stackAndQueue;

import java.util.LinkedList;

/**
 * 题目:有一个整形数组,arr和一个大小为w的窗口从数组的最左边滑到最右边,窗口每次向右滑动一个位置。
 * 例如数组[4,3,5,4,3,3,6,7],窗口的大小为3时;
 * [4 3 5] 4 3 3 6 7     窗口中最大值为5
 * 4[ 3 5 4] 3 3 6 7     窗口中最大值为5
 * 4 3 [5 4 3] 3 6 7     窗口中最大值为5
 * 4 3 5 [4 3 3 ]6 7     窗口中最大值为4
 * 4 3 5 4 [3 3 6] 7     窗口中最大值为6
 * 4 3 5 4 3 [3 6 7 ]    窗口中最大值为7
 * 如果数组的长度为n,窗口大小为w,则一共产生n-w+1个窗口的最大值
 * 请实现一个函数  
 *  输入:整数数组为arr,窗口大写为W。
 *  输出:一个长度为n-w+1的数组res,res[i]表示每一种窗口状态下的最大值,以本题为例,结果应该返回{5,5,5,4,6,7}
 */
public class GetMaxWindow {
    public static void main(String[] args) {
    	 int [] arr1={4,3,5,4,3,3,6,7};
    	 int w=3;
    	GetMaxWindow window=new GetMaxWindow();
    	arr1=window.getMaxWindow1(arr1, w);
    	for(int a:arr1){
    		System.out.println(a);
    	}
    	System.out.println("---------------------------------------");
    	 i