209. Minimum Size Subarray Sum LeetCode Solution

In this guide, you will get 209. Minimum Size Subarray Sum LeetCode Solution with the best time and space complexity. The solution to Minimum Size Subarray Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Size Subarray Sum solution in C++
  4. Minimum Size Subarray Sum solution in Java
  5. Minimum Size Subarray Sum solution in Python
  6. Additional Resources
209. Minimum Size Subarray Sum LeetCode Solution image

Problem Statement of Minimum Size Subarray Sum

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 target. 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

Constraints:

1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 104

Follow up: If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)).

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

209. Minimum Size Subarray Sum LeetCode Solution in C++

class Solution {
 public:
  int minSubArrayLen(int target, vector<int>& nums) {
    int ans = INT_MAX;
    int sum = 0;

    for (int l = 0, r = 0; r < nums.size(); ++r) {
      sum += nums[r];
      while (sum >= target) {
        ans = min(ans, r - l + 1);
        sum -= nums[l++];
      }
    }

    return ans == INT_MAX ? 0 : ans;
  }
};
/* code provided by PROGIEZ */

209. Minimum Size Subarray Sum LeetCode Solution in Java

class Solution {
  public int minSubArrayLen(int target, int[] nums) {
    int ans = Integer.MAX_VALUE;
    int sum = 0;

    for (int l = 0, r = 0; r < nums.length; ++r) {
      sum += nums[r];
      while (sum >= target) {
        ans = Math.min(ans, r - l + 1);
        sum -= nums[l++];
      }
    }

    return ans == Integer.MAX_VALUE ? 0 : ans;
  }
}
// code provided by PROGIEZ

209. Minimum Size Subarray Sum LeetCode Solution in Python

class Solution:
  def minSubArrayLen(self, target: int, nums: list[int]) -> int:
    ans = math.inf
    summ = 0
    j = 0

    for i, num in enumerate(nums):
      summ += num
      while summ >= target:
        ans = min(ans, i - j + 1)
        summ -= nums[j]
        j += 1

    return 0 if ans == math.inf else ans
# code by PROGIEZ

Additional Resources

See also  1206. Design Skiplist LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.