Data Structures19 sections · 729 units
Open in Course

Variable Window Pattern

Dynamic sizing

Variable-size windows grow and shrink based on a condition. The pattern is:

1.1. Expand: move rightright forward, adding the new element to the window.

2.2. Shrink: while the window violates some constraint, move leftleft forward, removing elements.

3.3. Update: after each expansion, update your answer. Here's the trick: each element is added once and removed at most once. Even though there's a nested loop, total work is O(n)O(n).