Sliding window again


  • 0
    Z
    public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        int left = 0;
        int right = 0;
        int min = Integer.MAX_VALUE;
        int sum = 0;
        int temp = 0;
        for(right = 0; right < nums.length; right++){
            temp += nums[right];
            sum += nums[right];
            while(sum >= s){//Minimize the window length if sum is larger than s
                min = Math.min(right - left + 1, min);
                sum = sum - nums[left];
                left++;
            }
        }
        if(temp < s){
            return 0;
        }
        return min;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.