Given an array nums of positive integers and a positive integer target, return the length of the shortest contiguous subarray whose sum is greater than or equal to target. If no such subarray exists, return 0.
A contiguous subarray is a slice of consecutive elements from nums. You should slide a variable-size window across the array, expanding the right edge to grow the sum and shrinking the left edge whenever the window's sum is at least target, recording the minimum window length seen.
Input
nums: an array of positive integers
target: a positive integer threshold
Output
The length of the shortest contiguous subarray whose sum is >= target, or 0 if no such subarray exists.
Examples
Example 1
Inputnums = [1, 4, 1, 7, 3, 0, 2, 5], target = 10
Output2
Explanation: The subarray [7, 3] sums to 10 and has length 2; no shorter contiguous subarray reaches the target.
Example 2
Inputnums = [1, 2, 3, 4, 5], target = 11
Output3
Explanation: The subarray [3, 4, 5] sums to 12, which meets the threshold with length 3.
Example 3
Inputnums = [1, 1, 1, 1], target = 100
Output0
Explanation: The total of all elements is 4, which is below 100, so no subarray qualifies.
Example 4
Inputnums = [10], target = 5
Output1
Constraints
- 1 <= nums.length
- All elements of nums are positive integers.