forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path209.c
More file actions
30 lines (25 loc) · 618 Bytes
/
209.c
File metadata and controls
30 lines (25 loc) · 618 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <assert.h>
int minSubArrayLen(int s, int* nums, int numsSize) {
int i = 0, j = 0;
int sum = 0;
int min = numsSize + 1;
while (j < numsSize) {
sum += nums[j];
while (sum >= s) {
if (j - i + 1 < min) {
min = j - i + 1;
}
sum -= nums[i];
i++;
}
j++;
}
return min == numsSize + 1 ? 0 : min;
}
int main() {
int nums[] = { 2, 3, 1, 2, 4, 3 };
assert(minSubArrayLen(7, nums, sizeof(nums) / sizeof(nums[0])) == 2);
printf("all tests passed\n");
return 0;
}