Minimum Size Subarray Sum
viaLeetCode
Problem: Given an array of positive integers and a target sum, find the minimal length of a contiguous subarray whose sum is greater than or equal to the target. Return 0 if there is no such subarray.
Constraints: Array length up to 10^5, positive integers.
Example: target = 7, nums = [2,3,1,2,4,3] -> 2 (subarray [4,3]).
Approach: Sliding window: expand the right pointer to grow the window sum; whenever the window sum >= target, shrink from the left while updating the minimum length, since all elements are positive. O(n) time, O(1) space.
asked …