-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_product.h
88 lines (83 loc) · 1.71 KB
/
max_product.h
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* max_product.h
*
* Created on: 2014年11月5日
* Author: ivycui
*/
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <sys/time.h>
using namespace std;
/* 考虑边界和各种情况
* 整数包括正、负、0三种情况
* 1. 全正数
* 2. 全负数,奇数个,偶数个
* 3. 长度为1
* 4. 中间某个数字为0
* 5. 普通情况,正负0都有
*/
int MaxProduct(int A[], int n)
{
int* a = A;
int result = a[0];
int allp = 1;
int count = 0;
int pre = 0;
int post = 0;
////前面n-1的乘积(无符号)
//如果数组中有0出现,则分而治之
int* b = new int[n];
for(int i = 0 ; i < n ;i ++)
{
if(a[i] == 0)
{//如果数组中有0出现,则分而治之
if(i > 0)
pre = MaxProduct(a, i);
if(i < n - 1)
post = MaxProduct(a+i+1, n-i-1);
return pre > post ? pre : post;
}
else if(a[i] < 0)
{
count ++;
allp *= -a[i]; //负数变正数
}else
{
allp *= a[i];
}
b[i] = allp;//[0,i]的乘积(无符号)
}
//保证没有0
if((count & 1) == 0)
{//偶数个负号,选择全部数字相乘
return allp;
}
else
{//奇数个负号,结果为第一个或最后一个负数前后两部分乘积较大的一方
pre = post = a[0];
result = a[0];
for(int i = 0 ; i < n - 1; i++)
{//第一个负数,不能是第n-1个位置,第n-1个位置前面乘积无意义
if(a[i] < 0)
{
post= allp/b[i];
break;
}
}
//反方向,++记得是--
for(int i = n - 1; i >= 1; i--)
{//第二个负数,不能是第0个位置,第0个位置前面乘积无意义
if(a[i] < 0)
{
pre = b[i - 1];
break;
}
}
result = pre > post ? pre : post;
}
delete b;
return result;
}