Getting Started with the Blue Bridge Cup (25) Maximum Sliding Window (Two-Way Queue)

Welcome ===Follow===Like===Comment, learn together, and make progress together!

------Continue to update the series of algorithm examples of the Lanqiao Cup entry-------

If you also like Java and algorithms, welcome to subscribe to the column to learn and communicate together!

Your likes, attention, and comments are the driving force for my creation!

-------I hope my article is helpful to you-------

 Column: Blue Bridge Cup Series

1. Topic description

You are given an array of integers numswith a  k sliding window of size moving from the far left of the array to the far right of the array. k You can only see the numbers in the sliding window . The sliding window moves to the right only one bit at a time.

Returns the maximum value in the sliding window .

Example 1:

Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
 Output: [3,3,5,5,6,7]
 Explanation:
The maximum position of the 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: nums = [1], k = 1
 Output: [1]

 2. Problem-solving ideas

1. This question is a typical question of sliding windows, but the conventional thinking will make the time complexity relatively large and timeout , so the two-way queue ArrayDeque is used to optimize the algorithm.

2. First, the window is regarded as a container with a capacity of k, and the left side is the head, and it must be the largest, otherwise the queue will be emptied gradually to ensure that the head of the queue is the largest .

3. Put the array elements into the container first, and the first element can only be the position in the array that is just at the bottom of the container , such as

k=3,num={3,4,7,9,4,8}, only 7 can be used as the head element.

4. Finally, save and return the maximum value in the qualified window.

5. What is recorded is the subscript in the array .

3. Realize the code

 public int[] maxSlidingWindow(int[] nums, int k) {//滑动窗口最大值
        ArrayDeque <Integer>queue=new ArrayDeque<>();
        int L=nums.length;
        int index=0;//数组下标
        if (L==1)  return nums;
        int res[]=new int[L-k+1];//结果个数为L-k+1个
        for (int i=0;i<L;i++)
        {
         while (!queue.isEmpty()&&queue.peek()<i-k+1)//即使已经进入队列但是不能作为队头即移出
         queue.poll();
         while (!queue.isEmpty()&&nums[queue.peekLast()]<nums[i])//后续元素有大于队头即把容器全部清空
         queue.pollLast();
            queue.offer(i);//逐步放入容器
            if (i>=k-1)
            {
                res[index++]=nums[queue.peek()];//记录每次窗口移动最大的元素
            }
        }

        return res;
    }

 

It's not easy to post, I implore the big guys to raise your hands!


Likes: Likes are a kind of virtue, and they are the recognition of my creation by the bosses!


Comments: There is no white contact, it is the beginning of communication between you and me!


Collection: May you pick more, it is the appreciation of the bosses to me!

Guess you like

Origin blog.csdn.net/m0_55278347/article/details/129370667