Skip to content

Commit f0036b2

Browse files
Add 209 in c language
1 parent 98b6666 commit f0036b2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

c/209-minimum-size-subarray-sum.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Given an array of positive integers nums and a positive integer target, return
3+
the minimal length of a contiguous subarray of which the sum is greater than or
4+
equal to target
5+
6+
Space: O(1)
7+
Time: O(n)
8+
*/
9+
10+
int min(int a, int b) {
11+
return a<b?a:b;
12+
}
13+
14+
int minSubArrayLen(int target, int* nums, int numsSize){
15+
int len=0;
16+
int i=0, j=0;
17+
int cpt=0;
18+
while (j<numsSize) {
19+
cpt += nums[j];
20+
if (cpt>=target) {
21+
if (len==0)
22+
len = j-i+1;
23+
while (cpt-nums[i] >= target) {
24+
cpt -= nums[i];
25+
len = min(len, j-i);
26+
i++;
27+
}
28+
}
29+
j++;
30+
}
31+
return len;
32+
}

0 commit comments

Comments
 (0)