Skip to content

visual walkthrough

Minimum Size Subarray Sum

MediumVariable Window (Shrink While Valid)Reported at: MetaAmazonMicrosoft+4

Shrinks while the window is valid, the mirror of the longest-window template.

Solve on LeetCode

The idea

Because every number is positive, adding to the window can only raise the sum and removing can only lower it. So grow the window until it is big enough, then shrink it from the left for as long as it stays big enough.

Each number is added once and removed once.

Complexity

approachtimespace
Try every startO(n²)O(1)
Sliding windowO(n)O(1)

More walkthroughs