We use the sliding window (two-pointer) technique. We expand the window from the right until the sum is greater than or equal to target. Then we shrink the window from the left as much as possible while maintaining the sum condition, updating the min_length at each step.
- Initialize
left = 0,current_sum = 0, andmin_len = infinity. - Iterate
rightfrom 0 ton-1:current_sum += nums[right].- While
current_sum >= target:min_len = min(min_len, right - left + 1).current_sum -= nums[left].left += 1.
- If
min_lenwas never updated, return 0. - Return
min_len.
- Time Complexity: O(N), as each pointer visits each element once.
- Space Complexity: O(1), no extra storage.
def min_subarray_len(nums, target):
left = 0
current_sum = 0
min_len = float('inf')
for right in range(len(nums)):
current_sum += nums[right]
while current_sum >= target:
# Update minimum length
min_len = min(min_len, right - left + 1)
# Shrink from the left
current_sum -= nums[left]
left += 1
return min_len if min_len != float('inf') else 0