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 <= 1091 <= nums.length <= 1051 <= nums[i] <= 104Follow 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 Sliding Window approach keeps track of a window using two pointers and dynamically adjusts subarray boundaries to maintain valid subarrays with sums greater than or equal to the target.
1. Initialize two pointers, left and right, both set to the start of the array.
2. Use right to expand the window by including elements in the subarray until the sum is equal to or exceeds the target.
3. Once the sum reaches or exceeds the target, attempt to minimize the window from the left by incrementing left while ensuring the sum remains equal to or greater than the target.
In the C solution, we maintain a running sum of the elements included in the sliding window. We keep incrementing the right pointer to include elements into the subarray. Each time the sum equals or exceeds the target, we check for the minimal length of the valid subarray and decrement elements from the left, reducing the subarray size.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) where n is the length of the array.
Space Complexity: O(1) because we use a constant amount of extra space.
This approach uses prefix sums and binary search to find the minimal length of subarrays summing to the required target. It builds on the idea that the sum of elements from index i to j can be represented using prefix sums, allowing us to search for valid subarray boundaries efficiently.
1. Compute the prefix sums of the array, which helps us quickly calculate the sum of any subarray.
2. For each starting index, use binary search to find the smallest ending index such that the subarray sum equals or exceeds the target.
In this C solution, we utilize prefix sums and binary search to efficiently locate minimum subarray lengths that meet the target. After constructing the prefix sum array, binary search over this array finds boundaries within a subarray sum condition, reducing it to an efficient search problem.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to binary searching for each possible start index.
Space Complexity: O(n) for the prefix sums array.
| Approach | Complexity |
|---|---|
| Sliding Window Approach | Time Complexity: Space Complexity: |
| Binary Search on Prefix Sums | Time Complexity: Space Complexity: |
Subarray Sum Equals K - Prefix Sums - Leetcode 560 - Python • NeetCode • 266,725 views views
Watch 9 more video solutions →Practice Minimum Size Subarray Sum with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor