Leetcode 209: Minimum Size Subarray Sum
Given an array of positive integers and a target value, find the minimal length of a contiguous subarray whose sum is greater than or equal to the target. If no such subarray exists, return 0.
Problem
Approach
Steps
Complexity
Input: You are given a list of integers, `nums`, and an integer `target`.
Example: [target = 8, nums = [1, 4, 4, 2, 1, 5]]
Constraints:
• The value of target is between 1 and 10^9.
• The length of nums is between 1 and 100,000.
• Each element of nums is between 1 and 10^4.
Output: The output should be the minimal length of a subarray whose sum is greater than or equal to the target, or 0 if no such subarray exists.
Example: For target = 8 and nums = [1, 4, 4, 2, 1, 5], the output is 2.
Constraints:
Goal: To efficiently find the smallest subarray whose sum is greater than or equal to the target.
Steps:
• Start iterating over the array while maintaining a running sum of the elements.
• If the sum becomes greater than or equal to the target, update the minimal subarray length and reduce the sum by removing elements from the start of the subarray.
• Repeat until you have considered all subarrays.
Goal: The constraints ensure that the solution must handle large inputs efficiently.
Steps:
• The array length is between 1 and 100,000, requiring an efficient O(n) solution.
• Each element is between 1 and 10,000, which ensures the sum can grow large but remains manageable.
Assumptions:
• The input array contains only positive integers.
• Input: Example 1
• Explanation: In this example, the subarray `[4, 4]` is the smallest subarray whose sum is at least 8, and its length is 2.
• Input: Example 2
• Explanation: The subarray `[3, 4]` sums to 7, which is greater than or equal to the target 6, and it has the minimal length of 2.
• Input: Example 3
• Explanation: In this case, no subarray's sum can reach 12, so the answer is 0.
Approach: The approach uses a sliding window technique to find the minimal length of a subarray with a sum greater than or equal to the target.
Observations:
• Sliding window techniques are ideal for problems involving subarrays with conditions on their sum.
• By maintaining a running sum and sliding the window over the array, we can find the minimal subarray efficiently.
Steps:
• Iterate over the array, keeping track of a running sum.
• Whenever the sum exceeds or meets the target, update the minimum subarray length and shrink the window from the left.
• Return the smallest subarray length that satisfies the condition or 0 if no such subarray is found.
Empty Inputs:
• If the array is empty or the target is very large, the result will be 0.
Large Inputs:
• For large arrays, ensure the solution handles up to 100,000 elements efficiently.
Special Values:
• If the target is 1 and the array contains only 1s, the smallest subarray will always have length 1.
Constraints:
• Ensure that the solution works within the time limits for large arrays (O(n) time complexity).
int minSubArrayLen(int target, vector<int>& nums) {
int sum = 0, idx = 0, g = INT_MAX, bdx = 0;
while(idx < nums.size()) {
sum += nums[idx];
while(sum >= target) {
g = min(g, idx - bdx + 1);
sum -= nums[bdx];
bdx++;
}
idx++;
}
return g == INT_MAX? 0: g;
}
1 : Function Definition
int minSubArrayLen(int target, vector<int>& nums) {
Define the function `minSubArrayLen` that takes a target sum and an array of integers, `nums`, and returns the minimum length of a subarray whose sum is at least equal to the target.
2 : Variable Initialization
int sum = 0, idx = 0, g = INT_MAX, bdx = 0;
Initialize variables: `sum` to track the current sum of the window, `idx` as the right pointer of the window, `g` to store the minimum length of the subarray, and `bdx` as the left pointer of the window.
3 : Loop Iteration
while(idx < nums.size()) {
Start a while loop that iterates through the array with the right pointer `idx`.
4 : Sum Update
sum += nums[idx];
Add the current element of `nums` at index `idx` to the sum of the current subarray.
5 : Inner While Loop
while(sum >= target) {
Start an inner while loop that checks if the sum of the current subarray is greater than or equal to the target. If true, try to shrink the window.
6 : Window Update
g = min(g, idx - bdx + 1);
Update the minimum subarray length `g` with the current window length, which is `idx - bdx + 1`.
7 : Sum Update
sum -= nums[bdx];
Shrink the window by subtracting the value at the left pointer `bdx` from the current sum.
8 : Left Pointer Move
bdx++;
Move the left pointer `bdx` to the right to shrink the window.
9 : Right Pointer Move
idx++;
Move the right pointer `idx` to the next element, expanding the window.
10 : Return Statement
return g == INT_MAX? 0: g;
Return the minimum length of the subarray. If no valid subarray is found, return 0 (indicating no subarray meets the target sum).
Best Case: O(n), when the sum of elements exceeds the target early in the iteration.
Average Case: O(n), as we process each element exactly once.
Worst Case: O(n), in the worst case where the window moves across the entire array.
Description: The sliding window technique ensures that we only iterate over the array once, making the time complexity linear.
Best Case: O(1), as no additional space is required.
Worst Case: O(1), as the space complexity is constant, only requiring variables for sum and indexes.
Description: The space complexity is constant because we are only using a few variables to keep track of the running sum and indices.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus