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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 想到了一个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 的大作中提到】
: 想到了一个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 的大作中提到】
: 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 的大作中提到】
: 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.
相关阅读
现在是不是必须进internet公司啊大胆预测,Offer包袱会每年加价4W大家觉得pure storage怎么样?华尔街急需多名金融软件工程师,强项在JAVA, .NET or C# , 另外需要一位Informatica 专业人士向前辈请教一下Asana这个公司【zz】回国工作,朋友介绍还是100offer?L家Onsite面完了,是不是都让填一个面试反馈问卷?argue工资,是涨salary好还是多要点RSU?RSU是不是交税少?从技术角度来看,google确实第一再不团结,真他娘的要成低等种族了 (转载)G的一道Onesite题求问一个offer选择请问哪里能找到introduction to algorithms的答案Facebook drops Bing in favor of its own search tool请问EPIC的C#职位可以去吗?HireRight里residency history要写的多详细?如何在一个graph中找到最长封闭回路?如何设计一个fb的privacy framework/systemFB面经老东家的hr非叫我把假期用掉