MemotivaLeetCode Patterns Flashcards: Sliding Window, Fixed Size, Dynamic Size, Substring

How does a fixed-size sliding window work?

LeetCode Patterns Flashcards: Sliding Window, Fixed Size, Dynamic Size, Substring

Аудио-карточка · 0:30

Nortren·

How does a fixed-size sliding window work?

0:30

A fixed-size sliding window maintains exactly k elements. Start by computing the result for the first k elements. Then slide the window one position right by adding the new element entering from the right and removing the element leaving from the left, updating the result in O of one time per step. This avoids recomputing the entire window from scratch at each position. Total time is O of n. Use it when the problem specifies a fixed window size, such as "maximum average of subarray of length k" or "find all anagrams of a pattern in a string" where the window size equals the pattern length.
neetcode.io