sliding window

A long array A[] is given to you. There is a sliding window of size  w  which is moving from the very left of the array to the very right. You can only see the  w  numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:

The array is [1 3 -1 -3 5 3 6 7], and w is 3.

Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7
Input : A long array A[], and a window width w
Output : An array B[], B[i] is the maximum value of from A[i] to A[i+w-1]
Requirement : Find a good optimal way to get B[i]


The double-ended queue is the perfect data structure for this problem. It supports insertion/deletion from the front and back. The trick is to find a way such that the largest element in the window would always appear in the front of the queue. How would you maintain this requirement as you push and pop elements in and out of the queue?

Besides, you might notice that there are some redundant elements in the queue that we shouldn’t even consider about. For example, if the current queue has the elements: [10 5 3], and a new element in the window has the element 11. Now, we could have emptied the queue without considering elements 10, 5, and 3, and insert only element 11 into the queue.

A natural way most people would think is to try to maintain the queue size the same as the window’s size. Try to break away from this thought and try to think outside of the box. Removing redundant elements and storing only elements that need to be considered in the queue is the key to achieve the efficient O(n) solution below.


import java.io.*;
import java.util.*;
import java.util.Map.Entry;

/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 */

public class Solution {
    

  public static int[] slidingWindowMax(int[] nums, int k) 
  {
      int n = nums.length - k + 1;
      Deque queue = new ArrayDeque();
    
      int[] max = new int[n];
    for (int i = 0; i < nums.length; i++) {
        while(!queue.isEmpty() && nums[queue.getLast()] <= nums[i]) {
           queue.removeLast();
        }
      
      queue.addLast(i);
      if (i < k - 1) continue;
      while(!queue.isEmpty() && i - k >= queue.getFirst())
        queue.removeFirst();
      
      max[i - k + 1] = nums[queue.getFirst()];
    }
    
    return max;
  }

 
  
  public static void main(String[] args) {
          int[] nums = {1,3,-1,-3,5,3,6};
          int[] ret = slidingWindowMax(nums, 3);
          for (int i : ret) {
              System.out.println(i);
          }
    
  }

}


你可能感兴趣的:(slidingwindow,poketGems)