Day86 sliding window maximum

Posted by jokeruk on Sat, 19 Feb 2022 08:43:14 +0100

Give you an integer array nums, with a sliding window of size k moving from the leftmost side of the array to the rightmost side of the array. You can only see k numbers in the sliding window. The sliding window moves only one bit to the right at a time. Returns the maximum value in the sliding window

https://leetcode-cn.com/problems/sliding-window-maximum/

Example 1:

Input: num = [1,3, - 1, - 3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Explanation:
Maximum position of sliding window

[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

Example 2:

Input: num = [1], k = 1
Output: [1]

Example 3:

Input: num = [1, - 1], k = 1
Output: [1, - 1]

Example 4:

Input: num = [9,11], k = 2
Output: [11]

Example 5:

Input: num = [4, - 2], k = 2
Output: [4]

Tips:

1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length

Java solution

Idea:

  • The content of the topic is easy to understand, that is, the sliding array returns the maximum value in each scrolling window when scrolling

  • Preliminary assumption: when scrolling, maintain an array of the current window size, record the maximum value in order, and maintain and replace the removed value and moved in value during scrolling

    • Size maintenance: binary search and update of ordered array
    • Imagine updating: build a lot of maintenance for the size data of the current window when the maximum value is removed
  • Scenario 2: find the maximum value of the current sliding window, and the maximum value will not change until the scrolling exceeds the current sliding window (before moving into a larger value)

    • When removing the maximum value of the current window, look for the maximum value again

    • Able to complete but inefficient, timeout

      public static int[] maxSlidingWindow(int[] nums, int k) {
          //Move from 0
          int maxIndex = findMaxIndex(nums, 0, k);
          int length = nums.length;
          int moveStep = length - k+1;
          int[] maxNums = new int[moveStep];
          maxNums[0] = nums[maxIndex];
          for (int i = 1; i < moveStep; i++) {
              //The value moved in is i+k-1
              maxIndex = nums[maxIndex]>nums[i+k-1]?maxIndex:i+k-1;
              if (i>maxIndex) {
                  maxIndex = findMaxIndex(nums,i,k+i);
              }
              maxNums[i] = nums[maxIndex];
          }
      
          return maxNums;
      }
      
      
      public static int findMaxIndex(int[] nums, int start, int end) {
          int max = start;
          for (int i = start+1; i < end; i++) {
              max = nums[i]>=nums[max]?  i:max;//Because moving to the right, taking a larger value of index when all are equal can reduce the calculation steps
          }
          return max;
      }
      
  • Reference official solution: monotone queue

    • It can be regarded as the optimization of my method. If the elements n-1 and N are num [n-1] < num [n], the maximum value of n-1 in the window must not be it
    • Maintain queues according to this nature
package sj.shimmer.algorithm.m4_2021;

import java.util.Deque;
import java.util.LinkedList;

import sj.shimmer.algorithm.Utils;

/**
 * Created by SJ on 2021/4/23.
 */

class D86 {
    public static void main(String[] args) {
        Utils.logArray(maxSlidingWindow(new int[]{1,3,-1,-3,5,3,6,7},3));
        Utils.logArray(maxSlidingWindow(new int[]{1},1));
        Utils.logArray(maxSlidingWindow(new int[]{1,-1},1));
        Utils.logArray(maxSlidingWindow(new int[]{9,11},2));
        Utils.logArray(maxSlidingWindow(new int[]{4,-2},2));
    }
    public static int[] maxSlidingWindow(int[] nums, int k) {
            int n = nums.length;
            //The double ended queue stores the permanently removable data of the first k elements, strictly monotonically decreasing
            Deque<Integer> deque = new LinkedList<Integer>();
            for (int i = 0; i < k; ++i) {
                while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) {
                    deque.pollLast();
                }
                deque.offerLast(i);
            }

            int[] ans = new int[n - k + 1];
            ans[0] = nums[deque.peekFirst()];
            for (int i = k; i < n; ++i) {
                while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) {
                    deque.pollLast();
                }
                deque.offerLast(i);
                while (deque.peekFirst() <= i - k) {
                    deque.pollFirst();
                }
                ans[i - k + 1] = nums[deque.peekFirst()];
            }
            return ans;
    }  
}

Official solution

https://leetcode-cn.com/problems/sliding-window-maximum/solution/hua-dong-chuang-kou-zui-da-zhi-by-leetco-ki6m/

  1. Priority queue

    The solution I envision uses a large root heap to solve the maintenance problem

  2. Monotone queue

    Reference scheme: see above

  3. Blocking + pretreatment

    Mathematical knowledge,

Topics: Java Algorithm data structure leetcode