Simple java solution


  • 1
    R
    public class Solution {
        public int minSubArrayLen(int s, int[] nums) {
            int min=0;
            int sol=Integer.MAX_VALUE;
            int res=0;
            int sum=0;
            for(int i=0;i<nums.length;i++){
                sum=sum+nums[i];
                if(sum>=s){
                    while(i>min && sum-nums[min]>=s){
                        sum=sum-nums[min];
                        min=min+1;
                    }
                    if(i-min+1<sol){
                        sol=i-min+1;
                        res=sol;
                    }
                }
            }
            return res;
        }
    }

Log in to reply
 

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