warming up your workspace

Sliding window

Applies to: general, python

A sliding window keeps a contiguous range over a sequence, expanding and shrinking it while tracking something about its contents, so subarray and substring problems run in O(n) instead of O(n^2).

left = 0; best = 0
for right in range(len(s)):
    while invalid():
        left += 1
    best = max(best, right - left + 1)

See also: two-pointers, array