forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
152.c
36 lines (31 loc) · 767 Bytes
/
152.c
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
#include <stdio.h>
int maxProduct(int A[], int n) {
int i;
int min, max, ret;
ret = max = min = A[0];
for (i = 1; i < n; i++) {
int t_max = max;
if (max * A[i] > A[i])
max *= A[i];
else
max = A[i];
/* A[i] is negative*/
if (min * A[i] > max)
max = min * A[i];
if (min * A[i] < A[i])
min *= A[i];
else
min = A[i];
/* A[i] is negative*/
if (t_max * A[i] < min)
min = t_max * A[i];
if (max > ret)
ret = max;
}
return ret;
}
int main() {
int A[] = {1, -2, 3, -2, -3};
printf("%d\n", maxProduct(A, 5));
return 0;
}