f*i
2 楼
x = a0*a1...*an
find x1 = a0*a1...ai where ai is the first negative element
find x2 = an*an-1...aj where aj is the last negative element
if x is positive, then x,
if x is negative, then max(x/x1, x/x2,x1/ai,x2/aj)
if there are 0 in the array, then divide the array into different groups by
0 and applied the way mentioned above to each group.
it is an O(n) approach.
find x1 = a0*a1...ai where ai is the first negative element
find x2 = an*an-1...aj where aj is the last negative element
if x is positive, then x,
if x is negative, then max(x/x1, x/x2,x1/ai,x2/aj)
if there are 0 in the array, then divide the array into different groups by
0 and applied the way mentioned above to each group.
it is an O(n) approach.
m*q
3 楼
想到了一个DP解,不过要复杂一些:
pos[i]: 以i结尾的最大绝对值乘积的正值
neg[i]: 以i结尾的最大绝对值乘积的负值
pe[i]: 存在以i结尾的正值
ne[i]: 存在以i结尾的负值
max: 当前连续乘积的最大值
pos[i-1] * a[i] (a[i] > 0 && pe[i-1] != 0)
pos[i] = neg[i-1] * a[i] (a[i] < 0 && ne[i-1] != 0)
a[i] (otherwise)
pos[i-1] * a[i] (a[i] < 0 && pe[i-1] != 0)
neg[i] = neg[i-1] * a[i] (a[i] > 0 && ne[i-1] != 0)
a[i] (otherwise)
pe[i] = pos[i] > 0;
ne[i] = neg[i] < 0;
max = MAX(max, pos[i]);
可以优化到只需要O(1)的额外空间,时间复杂度也是O(n)。
by
【在 f*********i 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: x = a0*a1...*an
: find x1 = a0*a1...ai where ai is the first negative element
: find x2 = an*an-1...aj where aj is the last negative element
: if x is positive, then x,
: if x is negative, then max(x/x1, x/x2,x1/ai,x2/aj)
: if there are 0 in the array, then divide the array into different groups by
: 0 and applied the way mentioned above to each group.
: it is an O(n) approach.
pos[i]: 以i结尾的最大绝对值乘积的正值
neg[i]: 以i结尾的最大绝对值乘积的负值
pe[i]: 存在以i结尾的正值
ne[i]: 存在以i结尾的负值
max: 当前连续乘积的最大值
pos[i-1] * a[i] (a[i] > 0 && pe[i-1] != 0)
pos[i] = neg[i-1] * a[i] (a[i] < 0 && ne[i-1] != 0)
a[i] (otherwise)
pos[i-1] * a[i] (a[i] < 0 && pe[i-1] != 0)
neg[i] = neg[i-1] * a[i] (a[i] > 0 && ne[i-1] != 0)
a[i] (otherwise)
pe[i] = pos[i] > 0;
ne[i] = neg[i] < 0;
max = MAX(max, pos[i]);
可以优化到只需要O(1)的额外空间,时间复杂度也是O(n)。
by
【在 f*********i 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: x = a0*a1...*an
: find x1 = a0*a1...ai where ai is the first negative element
: find x2 = an*an-1...aj where aj is the last negative element
: if x is positive, then x,
: if x is negative, then max(x/x1, x/x2,x1/ai,x2/aj)
: if there are 0 in the array, then divide the array into different groups by
: 0 and applied the way mentioned above to each group.
: it is an O(n) approach.
d*l
4 楼
int MaxProduct(int a[], int n)
{
int maxp = 0, maxn = 0;
int ans = 0;
for(int i = 0; i < n; i++) {
if(!a[i]) maxp = maxn = 0;
else if(a[i] > 0) {
maxp = max(maxp*a[i], a[i]);
maxn = a[i]*maxn;
} else {
int t = maxn;
maxn = min(maxp*a[i], a[i]);
maxp = a[i]*t;
}
ans = max(ans, maxp);
}
return (n==1)?a[0]:ans;
}
【在 m**q 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 想到了一个DP解,不过要复杂一些:
: pos[i]: 以i结尾的最大绝对值乘积的正值
: neg[i]: 以i结尾的最大绝对值乘积的负值
: pe[i]: 存在以i结尾的正值
: ne[i]: 存在以i结尾的负值
: max: 当前连续乘积的最大值
:
: pos[i-1] * a[i] (a[i] > 0 && pe[i-1] != 0)
: pos[i] = neg[i-1] * a[i] (a[i] < 0 && ne[i-1] != 0)
: a[i] (otherwise)
{
int maxp = 0, maxn = 0;
int ans = 0;
for(int i = 0; i < n; i++) {
if(!a[i]) maxp = maxn = 0;
else if(a[i] > 0) {
maxp = max(maxp*a[i], a[i]);
maxn = a[i]*maxn;
} else {
int t = maxn;
maxn = min(maxp*a[i], a[i]);
maxp = a[i]*t;
}
ans = max(ans, maxp);
}
return (n==1)?a[0]:ans;
}
【在 m**q 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 想到了一个DP解,不过要复杂一些:
: pos[i]: 以i结尾的最大绝对值乘积的正值
: neg[i]: 以i结尾的最大绝对值乘积的负值
: pe[i]: 存在以i结尾的正值
: ne[i]: 存在以i结尾的负值
: max: 当前连续乘积的最大值
:
: pos[i-1] * a[i] (a[i] > 0 && pe[i-1] != 0)
: pos[i] = neg[i-1] * a[i] (a[i] < 0 && ne[i-1] != 0)
: a[i] (otherwise)
g*y
6 楼
对有0的情况,象你说的divide-and-conqueror.
没有0时,
1. 扫描a, 生成负数子集neg
2. neg数为偶数,return product of a[]
3. neg数是奇数,return max(product of a[0..最后一个负数前],
product of a[第一个负数后..N-1]
by
【在 f*********i 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: x = a0*a1...*an
: find x1 = a0*a1...ai where ai is the first negative element
: find x2 = an*an-1...aj where aj is the last negative element
: if x is positive, then x,
: if x is negative, then max(x/x1, x/x2,x1/ai,x2/aj)
: if there are 0 in the array, then divide the array into different groups by
: 0 and applied the way mentioned above to each group.
: it is an O(n) approach.
没有0时,
1. 扫描a, 生成负数子集neg
2. neg数为偶数,return product of a[]
3. neg数是奇数,return max(product of a[0..最后一个负数前],
product of a[第一个负数后..N-1]
by
【在 f*********i 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: x = a0*a1...*an
: find x1 = a0*a1...ai where ai is the first negative element
: find x2 = an*an-1...aj where aj is the last negative element
: if x is positive, then x,
: if x is negative, then max(x/x1, x/x2,x1/ai,x2/aj)
: if there are 0 in the array, then divide the array into different groups by
: 0 and applied the way mentioned above to each group.
: it is an O(n) approach.
相关阅读
生物医药找Industry,techniques和publication哪个更重要?有facebook的朋友可以内部推荐一下的吗?急问:check地址换了能用吗?关于epic电面Staff software engineer是什么title?vs2010 beta test 出来了 (转载)再发数列题准备Bloomberg的电话面试请教一道面试题问个问题post order traveral using interation面试的时候和Programmer能谈什么呢?计算机猥琐男(又称码工)的春天终于到来了 (转载)Western Digital is looking for Mechanical Engineer有没有phone interview后直接给offer的?看到一个公司的招聘统计信息security clearance是什么意思phone interview 30 minutes from HM.sort 10T 的数字,怎么设计实现?TI @Dallasoffer选择求助