Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Sliding window

The shortest valid window

Lesson · 10 min read · Sliding window · The shortest valid window

When validity survives growing rather than shrinking, the loop turns inside out: shrink while the window is valid, and record its length before every step in.

The loop, inside out

"Covers every letter" and "sum at least K with positive numbers" are the opposite kind of property: a run that contains a valid run is valid. Growing can only help, so the question is how short a valid window can be. Grow on the right until the window is valid; then, while it stays valid, record its length and drop an element from the left. The moment it stops being valid, go back to growing.

Two details differ from the longest window and both cause bugs. The answer is recorded inside the shrinking loop, before each removal, because after the loop the window is no longer valid. And the best starts at "none found", not at zero.

The shortest window of "aabcbcdbca" that holds a, b, c and d.

  1. Step 1 / 11
    s
    a0a1b2c3b4c5d6b7c8a9
    need4

    The string holds 4 kinds of letter: a, b, c, d. A window must contain all of them.

  2. Step 2 / 11
    s
    a0lo/hia1b2c3b4c5d6b7c8a9
    have1best—

    'a' enters; the window covers 1 of 4.

  3. Step 3 / 11
    s
    a0loa1hib2c3b4c5d6b7c8a9
    have1best—

    'a' enters; the window covers 1 of 4.

  4. Step 4 / 11
    s
    a0loa1b2hic3b4c5d6b7c8a9
    have2best—

    'b' enters; the window covers 2 of 4.

  5. Step 5 / 11
    s
    a0loa1b2c3hib4c5d6b7c8a9
    have3best—

    'c' enters; the window covers 3 of 4.

  6. Step 6 / 11
    s
    a0loa1b2c3b4hic5d6b7c8a9
    have3best—

    'b' enters; the window covers 3 of 4.

  7. Step 7 / 11
    s
    a0loa1b2c3b4c5hid6b7c8a9
    have3best—

    'c' enters; the window covers 3 of 4.

  8. Step 8 / 11
    s
    a0a1b2loc3b4c5d6hib7c8a9
    have3best6

    'd' enters; the window covers 4 of 4. It covers everything, so record 7, then 6 while dropping from the left; it stops covering when lo reaches 2. Best 6.

  9. Step 9 / 11
    s
    a0a1b2loc3b4c5d6b7hic8a9
    have3best6

    'b' enters; the window covers 3 of 4.

  10. Step 10 / 11
    s
    a0a1b2loc3b4c5d6b7c8hia9
    have3best6

    'c' enters; the window covers 3 of 4.

  11. Step 11 / 11
    s
    a0a1b2c3b4c5d6b7loc8a9hi
    have3best4

    'a' enters; the window covers 4 of 4. It covers everything, so record 8, then 7, then 6, then 5, then 4 while dropping from the left; it stops covering when lo reaches 7. Best 4.

The code

The positive-sum version, where the state is a running total and "valid" means the total has reached the target.

def shortest_subarray(nums, target):
    n = len(nums)
    best, lo, total = n + 1, 0, 0
    for hi, value in enumerate(nums):
        total += value
        while total >= target:            # shrink while it still qualifies
            best = min(best, hi - lo + 1)
            total -= nums[lo]
            lo += 1
    return 0 if best == n + 1 else best

The same code is the reference solution to Shortest subarray with a sum at least the target, where it is compiled and run against the tests in all six languages.

Longest and shortest side by side

Longest valid windowShortest valid window
Validity survivesshrinkinggrowing
Inner loop runswhile the window is invalidwhile the window is valid
Record the answerafter the inner loopinside the inner loop, before removing
Start the best at0none found (∞ or a flag)
Typical words"longest", "at most""shortest", "at least", "contains all"

For "cover the letters of t, with repeats", keep need[c] for each letter of t and the number of letters still short. It is the anagram counter of the fixed-window module, used here to decide when the window is valid.

Problems in this module