Minimum Size Subarray Sum
Minimum Size Subarray Sum
Given an array of positive integers nums and a target target, return the minimal length of a contiguous subarray whose sum is at least target. If there is no such subarray, return 0.
Function signature
func MinSubarrayLen(target int, nums []int) int
In Python, implement min_subarray_len(target, nums).
Example
target = 7
nums = [2,3,1,2,4,3]
output = 2 // [4,3]
Constraints
- 1 <= len(nums) <= 200_000
- 1 <= nums[i] <= 10_000
- 1 <= target <= 1_000_000_000
Aim for O(n) time.
Try it before coding
With target 8, trace [1,7,1]. Which two shortest windows tie? What should the function return for target 10 on the same input?
Check your reasoning
Both [1,7] and [7,1] qualify at length 2. With target 10 even the full sum is only 9, so the answer is 0.
Run tests to see results
No issues detected