Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1344 from julienChemillier/patch-31
Browse files Browse the repository at this point in the history
Add 209 in c language
  • Loading branch information
Ahmad-A0 authored Nov 2, 2022
2 parents 59f1fd2 + 299ddca commit 57ef53d
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions c/209-Minimum-Size-Subarray-Sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Given an array of positive integers nums and a positive integer target, return
the minimal length of a contiguous subarray of which the sum is greater than or
equal to target
Space: O(1)
Time: O(n)
*/

int min(int a, int b) {
return a<b?a:b;
}

int minSubArrayLen(int target, int* nums, int numsSize){
int len=0;
int i=0, j=0;
int cpt=0;
while (j<numsSize) {
cpt += nums[j];
if (cpt>=target) {
if (len==0)
len = j-i+1;
while (cpt-nums[i] >= target) {
cpt -= nums[i];
len = min(len, j-i);
i++;
}
}
j++;
}
return len;
}

0 comments on commit 57ef53d

Please sign in to comment.