Skip to content

Commit

Permalink
Add 209 in c language
Browse files Browse the repository at this point in the history
  • Loading branch information
julienChemillier authored Oct 27, 2022
1 parent 98b6666 commit f0036b2
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 f0036b2

Please sign in to comment.