Leetcode 209. Minimum Size Subarray Sum
Unveiling Efficiency: The Sliding Window Method
Table of contents
No headings in the article.
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 totarget
. If there is no such subarray, return0
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
The Brute Force Approach
Using the brute force approach in this question may cause timeout issue in Leetcode.
function minSubArrayLen(target, nums) {
let minLength = Infinity;
for (let i = 0; i < nums.length; i++) {
let sum = 0
for (let j = i; j < nums.length; j++) {
sum += nums[j];
if (sum >= target) {
subLength = j - i + 1;
minLength = Math.min(minLength, subLength)
break;
}
}
}
return minLength === Infinity ? 0 : minLength;
}
Redundant Calculations:
For each starting index
i
, the algorithm recalculates the sum of the subarray fromi
toj
for all possible ending indicesj
.As the
j
pointer moves forward, the sum of the subarray is repeatedly recalculated, including elements that were already included in previous subarrays.This leads to redundant calculations of the same subarray sums multiple times, increasing the time complexity significantly. The time complexity is O(n^2).
How to reduce the the redundant calculations and unnecessary iterations?
The Sliding window Approach
The sliding window approach is like looking through a window that moves along a line of data. Instead of checking every single group, you can use a sliding window.
Here's how it works:
Start at the beginning: You place your window at the start of the row.
Look inside the window: You check the elements within the window to see if they meet your criteria (equal or greater than the target).
Move the window: Slide the window along the row by one element. You're now looking at a different group of elements.
Repeat: Keep moving the window along the row, checking each group of elements. Each time you move the window, you're effectively checking a new group without having to start from scratch.
This method is useful for problems where you need to analyze consecutive elements in a sequence or array, like finding the longest increasing subsequence or the maximum sum subarray. It's efficient because it avoids redundant calculations and simplifies the problem into smaller, manageable chunks.
There are three questions to ask before we start:
What's inside the window?
How to move the start index?
How to move the end index?
To implement this approach to find the minimal length of a subarray whose sum is greater than or equal totarget.
The sum of elements in the window should be greater than or equal to
target.
Move the start index if the sum of elements in the window is greater than or equal to
target.
The end index is guided by the for loop index.
function minSubArrayLen(target, nums) {
let minLength = Infinity;
let start = 0;
let sum = 0;
for (let end = 0; end < nums.length; end++) {
sum += nums[end];
while(sum >= target) {
let subLength = end - start + 1;
minLength = Math.min(minLength,subLength);
sum -=nums[start++]
}
}
return minLength === Infinity ? 0 : minLength;
}
This approach is more efficient than the brute force approach as it iterates through the array only once, maintaining a sliding window of elements whose sum meets or exceeds the target. Therefore, it achieves a linear time complexity. The time complexity is O(n).
In Conclusion:
The sliding window method emerges as a useful solution when confronted with consecutive element-related issues within a sequence or array.