File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed
209_minimum_size_subarray_sum Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ all :
2
+ gcc -O2 -o test mini_size.c
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <limits.h>
4
+
5
+ static int minSubArrayLen (int s , int * nums , int numsSize )
6
+ {
7
+ int i , j , sum = 0 , min = INT_MAX ;
8
+ for (i = 0 , j = 0 ; i < numsSize ; i ++ ) {
9
+ sum += nums [i ];
10
+ if (sum >= s ) {
11
+ while (j <= i ) {
12
+ sum -= nums [j ++ ];
13
+ if (sum < s ) {
14
+ int interv = i - j + 2 ;
15
+ min = interv < min ? interv : min ;
16
+ if (min == 1 ) {
17
+ return 1 ;
18
+ }
19
+ break ;
20
+ }
21
+ }
22
+ }
23
+ }
24
+
25
+ return min == INT_MAX ? 0 : min ;
26
+ }
27
+
28
+ int main (int argc , char * * argv )
29
+ {
30
+ if (argc < 3 ) {
31
+ fprintf (stderr , "Usage: ./test sum n1 n2...\n" );
32
+ exit (-1 );
33
+ }
34
+
35
+ int i , count = argc - 2 ;
36
+ int sum = atoi (argv [1 ]);
37
+ int * nums = malloc (count * sizeof (int ));
38
+ for (i = 0 ; i < count ; i ++ ) {
39
+ nums [i ] = atoi (argv [i + 2 ]);
40
+ }
41
+ printf ("%d\n" , minSubArrayLen (sum , nums , count ));
42
+ return 0 ;
43
+ }
You can’t perform that action at this time.
0 commit comments