Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 672 Bytes

File metadata and controls

30 lines (25 loc) · 672 Bytes

Minimum Size Subarray Sum

Solution 1

/**
 * Question   : 209. Minimum Size Subarray Sum
 * Complexity : Time: O(n) ; Space: O(1)
 * Topics     : Sliding Window
 */
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int minLen = Integer.MAX_VALUE;
        int left = 0;
        int right = 0;
        int currSum = 0;

        while (right < nums.length) {
            currSum += nums[right++];

            while (currSum >= target) {
                minLen = Math.min(minLen, right - left);
                currSum -= nums[left++];
            }
        }

        return minLen == Integer.MAX_VALUE ? 0 : minLen;
    }
}