Backlog·queued
Minimum Size Subarray Sum
DifficultyMedium
PatternSliding Window
TrackDSA
tl;dr
Given an array of positive integers, nums, and a positive integer, target, find the minimum length of a contiguous subarray whose sum is greater than or equal to the target. If no such subarray is found, return 0.
full write-up
Constraints
- 1 ≤
target≤ 10⁹ - 1 ≤
nums.length≤ 10⁵ - 1 ≤
nums[i]≤ 10⁴
Examples
Sample Example 1
Input:
nums= [2, 3, 1, 2, 4, 3]target= 7
Output: 2
Explanation: The subarray [4, 3] has a sum of 7, which meets the target, and its length of 2 is the smallest among all valid subarrays.
Sample Example 2
Input:
nums= [1, 4, 4]target= 4
Output: 1
Explanation: The subarray [4] alone already meets the target sum of 4, so the minimum length is 1.
Sample Example 3
Input:
nums= [1, 1, 1, 1, 1, 1, 1, 1]target= 11
Output: 0
Explanation: The sum of the entire array is 8, which is less than the target of 11, so no valid subarray exists.