15% off all plans & roadmaps Use code
See plansLeetCode 209 · Solution & editorial
Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
Minimum Size Subarray Sum is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. The pattern covers fixed-size, variable-size, and character frequency matching window techniques
Master variable-size sliding window for condition-based problems like longest substring without repeating characters.
The reference solution runs in O(n) time and O(1) space.
Minimum Size Subarray Sum has been reported in interviews at Goldman Sachs, Amazon, Bloomberg, Facebook, Microsoft, ByteDance.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Minimum Size Subarray Sum is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.