My JAVA solution


  • 0
    Q
    public class Solution {
        public int minSubArrayLen(int s, int[] nums) {
            int count = 0;
            int minCount = 0;
            int index = 0;
            int startIndex = 0;
            int sum = 0;
            while (index<nums.length){
                sum = sum + nums[index]; //keep adding until sum >= s
                index++;
                count++;
                if (sum>=s){
                	if (count<minCount || minCount == 0){
                		minCount = count;
                	}
                	while (sum>s){//keep removing until sum<=s
                		sum = sum - nums[startIndex];
                		startIndex++;
                		count--;
                		if (sum>=s && count<minCount){ //if sum is still greater reduce count
                			minCount=count;
                		}
                	}
                }
                
            }
            return minCount;
        }
    }

Log in to reply
 

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