Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Minimum Size Subarray Sum

Sliding Windowmedium

Implement `minSubArrayLen(target, nums)` returning the length of the smallest contiguous subarray whose sum is at least `target`, or `0` if none exists. All values are positive.

Sample tests

Input: minSubArrayLen(7, [2,3,1,2,4,3])
Output: 2
Input: minSubArrayLen(4, [1,4,4])
Output: 1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Returning 0 versus the found length when no window qualifies.

Learning resources

  • MDN: String
Approach & explanation (try first)

A grow-then-shrink window finds the minimal qualifying length in O(n).

Loading...
⌘/Ctrl + Enter

Run your code to see results.