Practice

Minimum Size Subarray Sum

Module 11 · Sliding Window

Problem

Given a positive-integer array nums and a target, return the length of the shortest contiguous subarray whose sum is ≥ target, or 0 if none exists.

Examples

Example 1

Inputtarget = 7, nums = [2,3,1,2,4,3]Output2

Explanation. [4,3], sum 7

Example 2

Inputtarget = 4, nums = [1,4,4]Output1

Explanation. [4]

Example 3

Inputtarget = 11, nums = [1,1,1,1,1,1,1,1]Output0

Explanation. max possible sum is 8

Constraints

1 ≤ n ≤ 10⁵ · 1 ≤ target ≤ 10⁹ · all values positive.

Attempt it first

This is the dynamic-windows lesson's opening trace, made concrete. The window's size is now the unknown — you're hunting for the shortest window meeting a lower-bound condition, which is exactly the "shrink while valid, record the smallest" template. Notice which constraint makes the whole technique legal before you write a line.