Given an array of positive integers nums
and a positive integer target
, return the minimal length of a subarray whose sum is greater than or equal to target
. If there is no such subarray, return 0
instead.
Example 1:
Input: target = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: The subarray [4,3] has the minimal length under the problem constraint.
Example 2:
Input: target = 4, nums = [1,4,4] Output: 1
Example 3:
Input: target = 11, nums = [1,1,1,1,1,1,1,1] Output: 0
Constraints:
1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 104
Follow up: If you have figured out the
O(n)
solution, try coding another solution of which the time complexity is O(n log(n))
.The problem asks for the minimum length of a contiguous subarray within nums
whose sum is greater than or equal to target
. We'll explore two efficient solutions: a prefix sum approach with binary search and a two-pointer sliding window technique.
Approach:
Prefix Sum: Calculate the prefix sum array s
. s[i]
stores the sum of elements from nums[0]
to nums[i-1]
. This allows us to efficiently compute the sum of any subarray: the sum of nums[i...j]
is s[j+1] - s[i]
.
Iteration and Binary Search: Iterate through the prefix sum array. For each s[i]
, we want to find the smallest index j
such that s[j] >= s[i] + target
. This j
represents the end of a subarray that meets the target sum condition. We use binary search on the prefix sum array to find this j
efficiently.
Minimum Length: The length of the subarray is j - i
. We keep track of the minimum length found so far.
Time Complexity: O(n log n) - The prefix sum calculation is O(n), and the loop with binary search within each iteration is O(n log n).
Space Complexity: O(n) - We use a prefix sum array of size n+1.
Approach:
Initialization: Use two pointers, l
(left) and r
(right), to define a sliding window. Initialize l
and r
to 0. Maintain a running sum s
initialized to 0.
Sliding Window: Move the right pointer r
to expand the window, adding each element to s
.
Window Condition: When s >= target
, the window contains a subarray that satisfies the condition. Record the current window length (r - l + 1
) as a potential minimum length.
Shrinking Window: To find a potentially shorter subarray, start shrinking the window from the left by moving l
to the right. Subtract nums[l]
from s
each time. Continue shrinking as long as s >= target
.
Repeat: Continue this expand-and-shrink process until r
reaches the end of the array.
Time Complexity: O(n) - Each element is added and removed from the window at most once.
Space Complexity: O(1) - Constant extra space is used.
Solution 1 (Prefix Sum + Binary Search):
import bisect
from itertools import accumulate
def minSubArrayLen(target, nums):
n = len(nums)
s = list(accumulate(nums, initial=0)) # Prefix sum array
ans = n + 1
for i in range(n + 1):
j = bisect.bisect_left(s, s[i] + target) #Binary search for j
if j <= n:
ans = min(ans, j - i)
return ans if ans <= n else 0
Solution 2 (Two Pointers):
import math
def minSubArrayLen(target, nums):
l = s = 0
ans = math.inf # Initialize with infinity
for r, x in enumerate(nums):
s += x
while s >= target:
ans = min(ans, r - l + 1)
s -= nums[l]
l += 1
return 0 if ans == math.inf else ans
The two-pointer solution is generally preferred due to its superior time complexity. However, the prefix sum approach might be easier to understand conceptually for some. Choose the solution that best suits your understanding and coding style. The code examples in other languages (Java, C++, Go, TypeScript etc) would follow similar logic and structure, primarily differing in syntax.