forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve.c
More file actions
38 lines (38 loc) · 659 Bytes
/
solve.c
File metadata and controls
38 lines (38 loc) · 659 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
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline int MAX(int a, int b)
{
return a > b ? a : b;
}
int maxSubArray(int *a, int n)
{
if (a == NULL || n <= 0)
return 0;
int max = a[0], sum = a[0];
for (int i = 1; i < n; ++i) {
if (sum < 0)
sum = a[i];
else
sum += a[i];
max = MAX(max, sum);
}
return max;
}
void print(int *a, int n)
{
for (int i = 0; i < n; ++i)
printf("%d ", a[i]);
printf("\n");
}
int main(int argc, char **argv)
{
int a[20], n;
memset(a, 0, sizeof(a));
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
printf("%d\n", maxSubArray(a, n));
}
return 0;
}