209. Minimum Size Subarray Sum
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum length of a contiguous subarray within a given array of positive integers, such that the sum of the elements in this subarray is greater than or equal to a specified target value. If no such subarray exists, the function should return 0.
Solution
Minimum Size Subarray Sum in Python
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
n = len(nums)
min_length = float('inf') # Initialize min_length to infinity.
left = 0 # The left pointer of the sliding window.
current_sum = 0 # The sum of elements in the current window.
# The right pointer of the sliding window iterates through the array.
for right in range(n):
# Expand the window by adding the current element to the sum.
current_sum += nums[right]
# While the window sum is valid (>= target), try to shrink it from the left.
while current_sum >= target:
# Update the minimum length found so far.
min_length = min(min_length, right - left + 1)
# Shrink the window by removing the leftmost element and moving the left pointer.
current_sum -= nums[left]
left += 1
# If min_length was never updated, it means no valid subarray was found.
# Otherwise, return the minimum length found.
return min_length if min_length != float('inf') else 0
Minimum Size Subarray Sum in Java
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
int minLength = Integer.MAX_VALUE; // Initialize minLength to a very large value.
int left = 0; // The left pointer of the sliding window.
long currentSum = 0; // Use long for the sum to prevent potential overflow.
// The right pointer of the sliding window iterates through the array.
for (int right = 0; right < n; right++) {
// Expand the window by adding the current element to the sum.
currentSum += nums[right];
// While the window sum is valid (>= target), try to shrink it from the left.
while (currentSum >= target) {
// Update the minimum length found so far.
minLength = Math.min(minLength, right - left + 1);
// Shrink the window by removing the leftmost element and moving the left pointer.
currentSum -= nums[left];
left++;
}
}
// If minLength was never updated, it means no valid subarray was found.
// Otherwise, return the minimum length found.
return (minLength == Integer.MAX_VALUE) ? 0 : minLength;
}
}
Minimum Size Subarray Sum in C++
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n = nums.size();
int minLength = INT_MAX; // Initialize minLength to a very large value.
int left = 0; // The left pointer of the sliding window.
long long currentSum = 0; // Use long long for the sum to prevent overflow.
// The right pointer of the sliding window iterates through the array.
for (int right = 0; right < n; ++right) {
// Expand the window by adding the current element to the sum.
currentSum += nums[right];
// While the window sum is valid (>= target), try to shrink it from the left.
while (currentSum >= target) {
// Update the minimum length found so far.
minLength = min(minLength, right - left + 1);
// Shrink the window by removing the leftmost element and moving the left pointer.
currentSum -= nums[left];
left++;
}
}
// If minLength was never updated, it means no valid subarray was found.
// Otherwise, return the minimum length found.
return (minLength == INT_MAX) ? 0 : minLength;
}
};
Minimum Size Subarray Sum in TypeScript
function minSubArrayLen(target: number, nums: number[]): number {
let n = nums.length;
let minLength = Infinity; // Initialize minLength to infinity.
let left = 0; // The left pointer of the sliding window.
let currentSum = 0; // The sum of elements in the current window.
// The right pointer of the sliding window iterates through the array.
for (let right = 0; right < n; right++) {
// Expand the window by adding the current element to the sum.
currentSum += nums[right];
// While the window sum is valid (>= target), try to shrink it from the left.
while (currentSum >= target) {
// Update the minimum length found so far.
minLength = Math.min(minLength, right - left + 1);
// Shrink the window by removing the leftmost element and moving the left pointer.
currentSum -= nums[left];
left++;
}
}
// If minLength was never updated, it means no valid subarray was found.
// Otherwise, return the minimum length found.
return minLength === Infinity ? 0 : minLength;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.