Last updated on February 2nd, 2025 at 05:53 am
Here, we see a Minimum Size Subarray Sum LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Array, Binary Search, Two Pointers
Companies
Level of Question
Medium

Minimum Size Subarray Sum LeetCode Solution
Table of Contents
1. Problem Statement
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, 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
2. Coding Pattern Used in Solution
The coding pattern used in the provided code is a Sliding Window. This pattern is commonly used for problems involving contiguous subarrays or substrings, where the goal is to find a subarray that satisfies a certain condition (e.g., minimum length, maximum sum, etc.).
3. Code Implementation in Different Languages
3.1 Minimum Size Subarray Sum C++
class Solution { public: int minSubArrayLen(int target, vector<int>& nums) { int l = 0, r = 0, n = nums.size(), sum = 0, len = INT_MAX; while (r < n) { sum += nums[r++]; while (sum >= target) { len = min(len, r - l); sum -= nums[l++]; } } return len == INT_MAX ? 0 : len; } };
3.2 Minimum Size Subarray Sum Java
class Solution { public int minSubArrayLen(int target, int[] nums) { if (nums == null || nums.length == 0) return 0; int i = 0, j = 0, sum = 0, min = Integer.MAX_VALUE; while (j < nums.length) { sum += nums[j++]; while (sum >= target) { min = Math.min(min, j - i); sum -= nums[i++]; } } return min == Integer.MAX_VALUE ? 0 : min; } }
3.3 Minimum Size Subarray Sum JavaScript
var minSubArrayLen = function(target, nums) { let left = 0; let right = 0; let sum = 0; let result = 0; while(right < nums.length) { sum += nums[right]; while(sum >= target) { let len = right - left +1; if(result === 0 || len < result) result = len; sum -= nums[left]; left++ } right++; } return result; };
3.4 Minimum Size Subarray Sum Python
class Solution(object): def minSubArrayLen(self, target, nums): total = left = right = 0 res = len(nums) + 1 while right < len(nums): total += nums[right] while total >= target: res = min(res, right-left+1) total -= nums[left] left += 1 right += 1 return res if res <= len(nums) else 0
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(n) | O(1) |
Java | O(n) | O(1) |
JavaScript | O(n) | O(1) |
Python | O(n) | O(1) |
- Sliding Window is efficient for problems involving contiguous subarrays.
- The algorithm ensures that each element is processed at most twice (once when expanding the window and once when shrinking it).
- The space complexity is O(1) because no additional data structures are used, only a few variables.