Minimum Size Subarray Sum

Medium#209variable size

You get an array of positive numbers and a target. Find the length of the shortest run of numbers in a row whose sum is at least the target, or 0 if there is none. Our example: [3, 1, 4, 2, 6, 5, 1] and target = 11 — the answer is 2: 6 + 5 = 11.

input
target: a positive integer; nums: an array of positive integers.
returns
The smallest length of a contiguous subarray of nums whose sum is at least target, or 0 if there is none.
constraints
  • 1 ≤ target ≤ 10⁹
  • 1 ≤ nums.length ≤ 10000
  • 1 ≤ nums[i] ≤ 10⁴
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number} target
 * @param {number[]} nums
 * @return {number}
 */
function minSubArrayLen(target, nums) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more sliding window problems

Pattern “Sliding window” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.