ConceptDifficulty: Intermediate2 min

Sliding Window: A Running Answer for a Contiguous Run

Grow the run on the right, shrink it from the left only while it breaks the rule, and every element enters and leaves once.

The sliding window pattern keeps a running answer, such as a sum or a set of counts, for a contiguous run of an array, and updates it as the run moves instead of recomputing it. A contiguous run is a stretch of neighbouring elements with none skipped, and the window is the run between two indices, start and end. The cue is a property of the problem: the answer is a contiguous run, and taking in one more element can only push the run towards breaking its rule, never back. A sum of non-negative costs that must stay within a budget has that property, because adding a cost can only raise the sum.

A fixed window keeps the same length and slides one step at a time. A variable window grows by moving end to the right and shrinks by moving start to the right, only while the rule is broken. Its invariant is that the window between start and end always satisfies the rule once the shrinking stops.

Daily costs of 2 1 3 1 1 1 4 must be covered from a budget of 6, and the question is the longest run of consecutive days that fits.

How long is the longest run of days that costs at most 6?

4 days. The run 1 3 1 1 reaches it first, and 3 1 1 1 matches it. The trace shows the window growing and shrinking.

A variable window over 2 1 3 1 1 1 4 with a budget of 6

Inputdaily costs 2 1 3 1 1 1 4, budget 6

  1. End 0

    1. 2startend
    2. 1
    3. 3
    4. 1
    5. 1
    6. 1
    7. 4

    took 2: window 2, sum 2, best 1

  2. End 1

    1. 2start
    2. 1end
    3. 3
    4. 1
    5. 1
    6. 1
    7. 4

    took 1: window 2 1, sum 3, best 2

  3. End 2

    1. 2start
    2. 1
    3. 3end
    4. 1
    5. 1
    6. 1
    7. 4

    took 3: window 2 1 3, sum 6, best 3

  4. End 3

    1. 2
    2. 1start
    3. 3
    4. 1end
    5. 1
    6. 1
    7. 4

    took 1, sum 7 over budget: dropped 2, window 1 3 1, sum 5, best 3

  5. End 4

    1. 2
    2. 1start
    3. 3
    4. 1
    5. 1end
    6. 1
    7. 4

    took 1: window 1 3 1 1, sum 6, best 4

  6. End 5

    1. 2
    2. 1
    3. 3start
    4. 1
    5. 1
    6. 1end
    7. 4

    took 1, sum 7 over budget: dropped 1, window 3 1 1 1, sum 6, best 4

  7. End 6

    1. 2
    2. 1
    3. 3
    4. 1
    5. 1start
    6. 1
    7. 4end

    took 4, sum 10 over budget: dropped 3 and 1, window 1 1 4, sum 6, best 4

ResultThe longest run within the budget is 4 days.

Start only ever moves right, and so does end, so each element enters the window once and leaves it at most once. The pass is O(n) time for n elements and O(1) extra space for a sum, against O(n²) for trying every start and extending it.

A caterpillar on a branch moves the same way: the head reaches forward and the tail catches up. The likeness breaks at length. A caterpillar stays about the same length, while a variable window can stretch over the whole array or shrink to nothing.

The pattern rests on the rule moving one way. With negative costs, dropping the leftmost element can raise the sum, and the shrinking step no longer finds the right window.