1ms O(n) Java solution, beats 99.76%


  • 0
    C
    
    public class Solution {
        public int minSubArrayLen(int s, int[] nums) {
            int i=0, j=0, min=nums.length+1, sum=0;
            while(j<nums.length){
                sum+=nums[j++]; 
                if(j-i>=min){
                    sum-=nums[i++];
                }
                while(sum>=s){
                    min=Math.min(j-i,min);
                    sum-=nums[i++];
                }
            }
            return min<=nums.length?min:0;
        }
    }

Log in to reply
 

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