Top Interview 150
The Minimum Size Subarray Sum problem is an excellent example of sliding window and binary search techniques. Letβs tackle this problem and explore both O(n) and O(nlogn) solutions.
π Problem Description
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
targe
t. - If no such subarray exists, return
0
.
π‘ Examples
Example 1
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray `[4,3]` has a sum of 7, which is the minimal length.
Example 2
Input: target = 4, nums = [1,4,4]
Output: 1
Explanation: The subarray `[4]` has a sum of 4, which is the minimal length.
Example 3
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
Explanation: No subarray meets the condition.
π JavaScript Solutions
Approach 1: Sliding Window (O(n))
The sliding window technique allows us to dynamically adjust the window size as we traverse the array.
Implementation
var minSubArrayLen = function(target, nums) {
let left = 0;
let sum = 0;
let minLength = Infinity;
for (let right = 0; right < nums.length; right++) {
sum += nums[right];
while (sum >= target) {
minLength = Math.min(minLength, right - left + 1);
sum -= nums[left];
left++;
}
}
return minLength === Infinity ? 0 : minLength;
};
π How It Works
-
Expand the Window:
- Add elements to the
sum
by moving theright
pointer.
- Add elements to the
-
Shrink the Window:
- When the
sum
becomes greater than or equal totarget
, update theminLength
and shrink the window by moving theleft
pointer.
- When the
-
Edge Case:
- If no subarray meets the condition, return
0
.
- If no subarray meets the condition, return
Complexity Analysis
- Time Complexity:
O(n)
, where n is the length of the array.- Each element is processed at most twice (once by
right
and once byleft
).
- Each element is processed at most twice (once by
- Space Complexity:
O(1)
, as we use constant extra space.
Approach 2: Binary Search (O(nlogn))
An alternative approach uses binary search to find the minimal subarray for each possible starting point.
Implementation
var minSubArrayLen = function(target, nums) {
const n = nums.length;
const prefixSum = Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) {
prefixSum[i] = prefixSum[i - 1] + nums[i - 1];
}
let minLength = Infinity;
for (let i = 0; i < n; i++) {
const desiredSum = prefixSum[i] + target;
let left = i + 1, right = n;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (prefixSum[mid] >= desiredSum) {
minLength = Math.min(minLength, mid - i);
right = mid - 1;
} else {
left = mid + 1;
}
}
}
return minLength === Infinity ? 0 : minLength;
};
π How It Works
-
Prefix Sum:
- Compute a prefix sum array where
prefixSum[i]
is the sum of the first i elements.
- Compute a prefix sum array where
-
Binary Search:
- For each starting index
i
, find the smallest ending indexj
such thatprefixSum[j] - prefixSum[i] >= target
.
- For each starting index
-
Update Result:
- Update
minLength
with the smallest valid subarray length.
- Update
Complexity Analysis
- Time Complexity:
O(nlogn)
, due to the binary search for each starting index. - Space Complexity:
O(n)
, for the prefix sum array.
π Dry Run
Input: target = 7
, nums = [2,3,1,2,4,3]
Sliding Window Steps:
Output: 2
β¨ Pro Tips for Interviews
- Explain Sliding Window: Highlight its efficiency in adjusting window size dynamically.
-
Discuss Edge Cases:
-
nums
with a single element equal totarget
. - Subarrays with all elements smaller than
target
.
-
Follow-Up: Mention how binary search is an alternative if asked for optimization.
π Learn More
Check out the previous full explanation and code walkthrough on my Dev.to post:
π 3Sum - JavaScript Solution
Which approach would you use? Letβs discuss below! π
Top comments (1)
Follow Me on GitHub π
If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.
Don't forget to follow for more updates!