my solution in java 1 ms


  • 0
    S
    
    public class Solution {
        public int minSubArrayLen(int s, int[] nums) {
            
            int curMinLength = Integer.MAX_VALUE;
            int currSum = 0;
            
            int i = 0;
            int j = 0;
            
            if (nums.length == 0)
                return 0;
            
            while(j < nums.length){
                if (currSum >= s){
                    curMinLength = curMinLength > i-j ? i-j : curMinLength;
                    currSum -= nums[j];
                    j++;
                }else if (currSum < s){
                    if (i < nums.length) {
                        currSum += nums[i];
                        i++;
                    }else {
                        currSum -= nums[j];
                        j++;
                    }
                }
            }
            return curMinLength == Integer.MAX_VALUE ? 0 : curMinLength;
        }
    }
    
    

Log in to reply
 

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