Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to calculate the maximum value of sliding window by leetcode

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces leetcode how to find the maximum value of the sliding window, the article introduces in great detail, has a certain reference value, interested friends must read it!

Given an array nums, a sliding window of size k moves from the far left of the array to the far right of the array. You can only see the numbers in the sliding window k. The sliding window moves only one bit to the right at a time.

Returns the maximum value of the sliding window.

Example:

Enter: = And = 3 output: maximum position of sliding window-- [1 3-1]-3 53 6 7 3 1 1 [3-1-3] 5 3 6 7 3 3 3 [- 1-3 5] 3 6 7 5 1 3-1 [- 3 53] 6 7 5 1 3-1-3 [5 36] 7 6 1 3-1-3 5 [3 6 7] 7

Note:

You can assume that k is always valid, 1 ≤ k ≤ input the size of the array, and the input array is not empty.

Advanced:

Can you solve this problem within linear time complexity?

Ideas for solving the problem:

1. Sliding window + big root heap will not work, because when the element on the left side of the window is moved out of the window, it does not know where it is on the heap and will damage the heap

2, double-ended queue (descending order of elements within the queue)

A, if the current element is larger than the first element of the queue, it means that the element that is still in the window is meaningless (not the maximum). Clear the queue and put the element at the head of the queue.

B, if the queue is full, remove the first element of the queue (to facilitate judgment, array subscripts are stored in the queue)

3. The queue is not full or 2.B:

A, if the current element is smaller than the end-of-line element, put the current element at the end of the line (possibly the maximum later)

B, if the current element is larger than the end-of-line element, pop up the end-of-line element (which cannot be the maximum value) until the current element is smaller than the end-of-line element, and place the current element at the end of the line

4. Pay attention to the boundary situation:

If the current element = 0) & (nums [I] > = nums [DQ [j]]); jmurf-{} if J0) & & (nums [I] > = nums [DQ [j]]) Fmt.Println-{} dq=append (DQ [0: jack1], I)} fmt.Println (dq) if I > = KMU1 {max=append (max,nums [dq [0])}} return max} above is all the content of the article "how to find the maximum value of sliding window". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report