← Visualizers
Sliding Window · variable window, sub-variant C — minimise LeetCode 209

Minimum Size Subarray Sum / visualized

This is the canonical minimise window, and the whole variant lives in one line's position. r always advances and adds. Then, while the window is already valid (sum ≥ target), you shrink from the left — and you record best inside that shrink loop, before each eviction, because the window is only as small as it will ever be at that instant. Move the record one line down, outside the loop, and you measure windows that have already been broken. The machine is only sound because every value is positive: adding on the right can only push the sum up, removing on the left can only push it down, so "valid" is monotone in the boundaries. LC 862 allows negatives, that monotonicity dies, and the sliding window dies with it — that problem needs a monotonic deque over prefix sums instead.

Execution

idle
nums  ·  teal cells are inside the window
window sum
Press Run to begin.
0 / 0
Speed

Java · running line

inside the window entering (sum +=) evicted (sum −=) shortest window recorded left behind
O(n) time · O(1) space  ·  each index enters once and leaves once, so the inner while is amortised O(1). Sub-variant C: record best inside the shrink loop. All values positive is not a detail — it is the precondition that makes the window work.