Super basic algorithm: sliding window

Introduction

Sliding windows, in simple terms, are a group of elements defined by dynamic boundaries. The size defined by the boundary is called the window size, and the magnitude of the boundary change is called the sliding step.

The application scenario of sliding window has several characteristics:

  1.  The results to be output or compared are arranged consecutively in the original data structure;
  2.  Every time the window slides, you only need to observe the changes of the elements at both ends of the window. No matter how long the window is, only two head and tail elements are operated at a time. When the window is relatively long, the number of operations can be significantly reduced;
  3.  The integrity of the elements in the window is relatively strong, and window sliding can be achieved only by operating the changes in the two positions of the head and the tail, but all the elements in the window are often used when comparing the results.

For example

The figure below shows a sliding window with a window size of 5 and a sliding step of 1.

The left boundary of window one is 1, and the right boundary is 5. It contains five elements of 1~5.

At the next moment, the entire window slides to the right by 1. At this time, the left boundary is 2 and the right boundary is 6, which contains five elements 2-6.

At the next moment, the entire window slides to the right by 1. At this time, the left boundary is 3 and the right boundary is 7, which contains five elements of 3~7. . .

Supplement : The size of the window is actually not fixed. In other words, when the right border slides to the right by 1, the left border can slide by 1, or not, and you can slide any value greater than 0 and less than the window size (if greater than The window size exceeds the right margin).

application

The following is the original question in a force button:

209. The smallest subarray

Given an array containing n positive integers and a positive integer s, find the continuous sub-array with the smallest length satisfying its sum ≥ s and return its length. If there is no continuous sub-array that meets the conditions, 0 is returned.

Example: 

Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: Sub-array [4,3] is the continuous sub-array with the smallest length under this condition

solution:

class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        int leftPoint = 0;
        int rightPoint = 0;
        int sum = 0 ;
        int result = Integer.MAX_VALUE;
        //当右指针小于数组的最大程度
        while (rightPoint<nums.length){
            //累计窗口中的所有元素
            sum = sum + nums[rightPoint];
            //如果sum值达到s值
            while (sum>=s){
                //更新result的值
                result = Math.min(rightPoint-leftPoint,result)
                //去除左边界的元素,如果仍然满足sum>=s的条件,则会再次判断,否则右指针向右滑动
                sum = sum-nums[leftPoint];
                leftPoint++;
            }
            rightPoint++;
        }
        return result == Integer.MAX_VALUE ? 0 : result+1;
    }
}

Usually the queue data type is very suitable for sliding windows, but the code uses double pointers to represent the left and right boundaries of the window.

  1. First slide the right pointer to the right, and the left pointer does not move. When the sum values ​​of all elements in the window indicated by the left and right pointers meet the conditions, update the minimum window size that meets the conditions, and try to move the left pointer to the right.
  2. After the left pointer moves to the right, judge again whether the condition is still met, and if it is still met, repeat the second step.
  3. Until the conditions are not met, continue with the first step.
  4. Finally return result.

Guess you like

Origin blog.csdn.net/x950913/article/details/107004484