AC java solution with comments


  • 0
    B
    public class Solution {
        public int minSubArrayLen(int s, int[] nums) {
            int result = nums.length + 1;
            int start, end , sum;
            start = end = sum = 0;
            for(int i=0;i<nums.length;i++){
                sum += nums[i];
                if(sum>=s){
                    end = i--;
                    result = Math.min(result,end - start +1);
                    sum -= nums[start++]; //shrink the window
                    sum -= nums[end]; //we keep the same last element and check the sum again for the next iteration
                }
            }
            if(result>nums.length)  return 0;
            return result;
        }
    }
    

Log in to reply
 

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