C,0ms,share my code


  • -2
    Y

    the basic idea:
    given you a array,assume the subarray [i,j] has the minimal length under the problem constraint.then Sum(array[i,h],h>j)>s.but the minimal length is j-i+1,so we just need to use len[] to store the minimal length
    from 1,2,3..numsSize.find the minimal lengh.

    int minSubArrayLen(int s, int* nums, int numsSize) {
        int i,j=0,sum=0;
        int* len=(int*)malloc(sizeof(int)*numsSize);
        memset(len,0,sizeof(int)*numsSize);
        for(i=0;i<numsSize;i++){
                sum+=nums[i];
                if(sum>=s){
                        do{
                                len[j]=i-j+1;
                                sum-=nums[j];
                                j++;
                        }while(sum>=s);
                }
        }
        if(j==0)
                return 0;
        int min=numsSize;
        for(i=0;i<j;i++)
                if(len[i]<min)
                        min=len[i];
        return min;
    

    }


Log in to reply
 

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