SLIDING WINDOW

What is the SLIDING WINDOW technique???

SLIDING WINDOW

A technique that involves maintaining a window of a fixed size while iterating through a data structure.

Gemini

SLIDING WINDOW

???

SLIDING WINDOW

Imagine you're watching a parade from a small window, but the parade is so long that you can't see everything at once.

We have a problem, we want to see the other parts of the parade too

What if we slide our window to another part of the street, that should work yeah ?

It’s a method to optimize the process of calculating something over a range of elements in an array or string.

SLIDING WINDOW

Instead of looking through the array one element at a time, we can form a range, i.e a subset of the array, and work on that subset first.

SLIDING WINDOW

Then we slide over to the next group.​

THERE IS MOREEE

Instead of recalculating values from scratch, the window (range) “slides” over the array input, reusing information from the previous step.

TIME COMPLEXITY

O(n)

TYPES OF SLIDING WINDOWS:

Fixed-size window

Dynamic-size window

Fixed-size window

The window size remains constant as it moves

Dynamic-size window

The window size can expand or shrink based on the problem conditions.

Setting up sliding window

(Dynamic)

Start with two pointers (left and right) at the beginning of the input.

Move the right pointer to expand the window (range), and when needed, move the left pointer to shrink it.

At each step, update the result based on the current window (range) size or content.

COMMON CHALLENGES

Correctly managing the window size.

Handling edge cases like empty arrays or all negative numbers.

Further Reading and Relevant Links

Inside Code

Mastering sliding window

by Megha B H

(With Visualization)

It's that time again, Leetcode o'clock !!!

Thank you !!!!

S.I.T: DSA - Sliding window

By Chineletam Ugwuadu (Letam)

S.I.T: DSA - Sliding window

  • 172