[Offer] [59-1] [] is the maximum value of the sliding window

Title Description

  Given an array of size and sliding window, find the maximum value of all sliding window. For example, if the input array {2,3,4,2,6,2, 5,1}, and the size of the sliding window 3, then the presence of a total of six sliding window, their maximum values ​​are {4,4,6, 6,6,5}, as shown in the following table:

Cattle brush off questions address network

Ideas analysis

  Establish a queue open at both ends, may be placed all the maximum number (in fact stored in the corresponding index), and the maximum at the beginning of the queue. Scan array from scratch,

  1. If the numbers come across all the figures are larger than the queue, then it is maximum, other numbers can not be the maximum, and all the numbers in the queue emptied into the number, which is located at the head of the queue;
  2. If the number after the encounter is less than all the numbers are in the queue, then it could become the maximum value of the sliding window, into the end of the queue;
  3. If the number is smaller than the maximum value encountered in the queue, minimum large, than it can not be a small number of the maximum, and delete the smaller number, put this number.
  4. Since the size of the sliding window, and therefore, if the head of the queue number which index distance from the end of the sliding window larger than the window size, the head of the queue delete numbers.
  5. Stored in the queue is the index number

Test Case

  1. Functional test: the input array size random number; monotonically increasing digital input array; digital input array decreases monotonically.
  2. Boundary value testing: sliding window size is 0, 1, equal to the length of the input array, is greater than the length of the input array.
  3. Special input test: the input array is empty.

Java code

public class Offer059_01 {
    public static void main(String[] args) {
        test1();
        test2();
        test3();

    }

    public static ArrayList<Integer> maxInWindows(int[] num, int size) {
        return Solution1(num, size);
    }

    private static ArrayList<Integer> Solution1(int[] num, int size) {
        ArrayList<Integer> max = new ArrayList<Integer>();
        if (num == null || num.length <= 0 || size <= 0 || size > num.length)
            return max;
        ArrayDeque<Integer> indexDeque = new ArrayDeque<Integer>();

        for (int i = 0; i < size - 1; i++) {
            while (!indexDeque.isEmpty() && num[i] > num[indexDeque.getLast()])
                indexDeque.removeLast();
            indexDeque.addLast(i);
        }

        for (int i = size - 1; i < num.length; i++) {
            while (!indexDeque.isEmpty() && num[i] > num[indexDeque.getLast()])
                indexDeque.removeLast();
            if (!indexDeque.isEmpty() && (i - indexDeque.getFirst()) >= size)
                indexDeque.removeFirst();
            indexDeque.addLast(i);
            max.add(num[indexDeque.getFirst()]);
        }

        return max;
    }

    private static void test1() {

    }
    private static void test2() {

    }
    private static void test3() {

    }

}

Code link

Offer to prove safety codes -Java

Guess you like

Origin www.cnblogs.com/haoworld/p/offer591-hua-dong-chuang-kou-de-zui-da-zhi.html