Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-197

Minimum Size Subarray Sum

Problem

Given positive integers nums and a target, return the minimal length of a contiguous subarray whose sum is at least target, or 0 if no such subarray exists.

Grow a right pointer and shrink from the left while the window sum meets the target, recording the best length. All values are positive.

Hint

Because all values are positive, shrinking the window never breaks a valid sum until it drops below target.

Test cases

case 1
in: [[2,3,1,2,4,3],7]
expected: 2
case 2
in: [[1,4,4],4]
expected: 1
case 3
in: [[1,1,1,1,1,1,1,1],11]
expected: 0
case 4
in: [[1,2,3],6]
expected: 3
case 5
in: [[],5]
expected: 0
Starter codePython
def min_size_subarray_sum(nums, target):
    # Your code here
    pass

5 test cases · run your code against them in the editor