108. Minimum Size Subarray Sum

Medium · Array

Given an array of positive integers and a target sum, find the length of the smallest contiguous subarray whose sum is greater than or equal to the target. If no such subarray exists, return 0.

This problem tests your ability to use the sliding window technique to optimize from a brute-force O(n²) approach to O(n).

Examples

Example 1
Input: nums = [2, 3, 1, 2, 4, 3], target = 7
Output: 2
Explanation: The subarray [4, 3] has the minimal length under the problem constraint. Its sum is 7, which equals the target.
Example 2
Input: nums = [1, 1, 1, 1, 1, 1], target = 11
Output: 0
Explanation: No subarray of any length has a sum ≥ 11, so return 0.

Constraints