Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2d26b19

Browse files
committed
"Minimum Size Subarray Sum"
1 parent 7283f65 commit 2d26b19

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
107107
| 212 | [Word Search II] | [C](src/212.c) |
108108
| 211 | [Add and Search Word - Data structure design] | [C](src/211.c) |
109109
| 210 | [Course Schedule II] | |
110-
| 209 | [Minimum Size Subarray Sum] | |
110+
| 209 | [Minimum Size Subarray Sum] | [C](src/209.c) |
111111
| 208 | [Implement Trie (Prefix Tree)] | [C](src/208.c) |
112112
| 207 | [Course Schedule] | |
113113
| 206 | [Reverse Linked List] | [C](src/206.c) |

‎src/209.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
#include <assert.h>
3+
4+
int minSubArrayLen(int s, int* nums, int numsSize) {
5+
int i = 0, j = 0;
6+
int sum = 0;
7+
int min = numsSize + 1;
8+
while (j < numsSize) {
9+
sum += nums[j];
10+
while (sum >= s) {
11+
if (j - i + 1 < min) {
12+
min = j - i + 1;
13+
}
14+
sum -= nums[i];
15+
i++;
16+
}
17+
j++;
18+
}
19+
20+
return min == numsSize + 1 ? 0 : min;
21+
}
22+
23+
int main() {
24+
int nums[] = { 2, 3, 1, 2, 4, 3 };
25+
assert(minSubArrayLen(7, nums, sizeof(nums) / sizeof(nums[0])) == 2);
26+
27+
printf("all tests passed\n");
28+
29+
return 0;
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /