Lintcode362 Sliding Window Maximum Solution

Source: Internet
Author: User

"Title description"

Given an array of n integers with duplicate number, and a moving window (size k), move the window at each iteration from the Start of the array, find the maximum number inside the window at each moving.

Give an array of integers that may contain duplicates, and a sliding window of size k , sliding the window from left to right in the array to find the maximum value within each window in the array.

"Topic link"

www.lintcode.com/en/problem/sliding-window-maximum/

"Problem Analysis"

Iterates over the array nums, using a double-ended queue deque to maintain an array subscript within the sliding window that is likely to be the largest element. Because each element in the array will only be enqueued and out of the queue at most, the averaging time complexity is O (n). The current subscript is I, then the active subscript range for the sliding window is [i-(k-1), I]. If the element in the deque is labeled I-(k-1), it pops from the team head, and the elements in the deque are queued in ascending order from the end of the queue. This ensures that the array subscript range in the deque is [i-(k-1), I], which satisfies the sliding window requirements.

When I queue up from the end of the team, the index of the array with the tail of the popup line is not more than nums[i] (these ejected elements become meaningless due to the addition of new elements). Deque's team head element is the maximum value of the current sliding window

"Reference Answer"

www.jiuzhang.com/solutions/sliding-window-maximum/



Lintcode362 Sliding Window Maximum Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.