Simple Java O(n) solution


  • 0
    C
    public int minSubArrayLen(int s, int[] nums) {
        if(nums.length==0) return 0;
        int p1=0, sum=0, min=nums.length;
        boolean possible=false;
        for(int i=0; i<nums.length; i++){
            sum+=nums[i];
            while(sum-nums[p1]>=s){
                sum-=nums[p1++];
            }
            if(sum>=s&&i-p1<min){
                min=i-p1;
                possible=true;
            }
        }
        return possible? min+1: 0;
    }

  • 0
    L
    This post is deleted!

  • 0
    S

    Short does not mean simple. its not easy to understand.


Log in to reply
 

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