Java 最大乘积子阵列问题
这是问题和代码(我搜索了解决方案,大多数都是相似的,发布了一篇易于阅读的文章),我的问题是下面两行Java 最大乘积子阵列问题,java,algorithm,Java,Algorithm,这是问题和代码(我搜索了解决方案,大多数都是相似的,发布了一篇易于阅读的文章),我的问题是下面两行 imax = max(A[i], imax * A[i]); imin = min(A[i], imin * A[i]); 为什么我们需要单独考虑一个[i],为什么不写为, imax = max(imin * A[i], imax * A[i]); imin = min(imin * A[i], imax * A[i]); 查找具有最大乘积的数组(至少包含一个数字)中的相邻子数组 例如
imax = max(A[i], imax * A[i]);
imin = min(A[i], imin * A[i]);
为什么我们需要单独考虑一个[i],为什么不写为,
imax = max(imin * A[i], imax * A[i]);
imin = min(imin * A[i], imax * A[i]);
查找具有最大乘积的数组(至少包含一个数字)中的相邻子数组
例如,给定数组[2,3,-2,4],
相邻子阵列[2,3]的最大乘积为6
int maxProduct(int A[], int n) {
// store the result that is the max we have found so far
int r = A[0];
// imax/imin stores the max/min product of
// subarray that ends with the current number A[i]
for (int i = 1, imax = r, imin = r; i < n; i++) {
// multiplied by a negative makes big number smaller, small number bigger
// so we redefine the extremums by swapping them
if (A[i] < 0)
swap(imax, imin);
// max/min product for the current number is either the current number itself
// or the max/min by the previous number times the current one
imax = max(A[i], imax * A[i]);
imin = min(A[i], imin * A[i]);
// the newly computed max value is a candidate for our global result
r = max(r, imax);
}
return r;
}
intmaxproduct(inta[],intn){
//存储到目前为止我们找到的最大值
int r=A[0];
//imax/imin存储以下各项的最大/最小乘积:
//以当前数字A[i]结尾的子阵列
对于(inti=1,imax=r,imin=r;i
提前感谢,,
林
当您单独考虑<代码> A[i] < /C> >时,基本上考虑到从<代码> [i] 开始的序列。 当您最初通过
a[0]
初始化imin
和imax
时,您也在做类似的事情
imin
情况也是如此
小例子:
Array={-4,3,8,5}
初始化:imin=-4,imax=-4
迭代1:i=1,A[i]=3
imax=max(A[i],imax*A[i])代码>->imax=max(3,-4*3)代码>->imax=3
因此,当imax
为负值,而A[i]
为正值时,A[i]
可以达到最大值。由于imax
和imin
的起始寿命都等于r
,您的更新建议将保持这两个值始终相等。不是你想要的。(我希望您知道,您发布的代码找到的是最大的乘积,而不是问题陈述中要求的子阵列本身)。@TedHopp,您认为我们可以通过只使用imax=max(imin*A[I]、max(A[I]、imax*A[I])和imin=min(imin*A[I]、min(A[I]、imax*A[I])来简化代码,而不需要使用if(A[I]<0)检查吗?谢谢。@LinMa-当A[i]
为负而imin
为正时,它当然可以!(-2大于3*-2,例如)。@LinMa我用一个小例子更新了答案,a[I]
可以在特定迭代中最大。@pgiitu,很好的例子@LinMa我希望你现在明白了。@pgiitu,你认为我们可以通过只使用imax=max(imin*A[I]、max(A[I]、imax*A[I])和imin=min(imin*A[I]、min(A[I]、imax*A[I])来简化代码,而不需要使用if(A[I]<0)检查吗?谢谢
public class MaximumContiguousSubArrayProduct {
public static int getMaximumContiguousSubArrayProduct(final int... array) {
if (array.length == 0) {
return -1;
}
int negativeMax = 0, positiveMax = 0, max;
if (array[0] < 0) {
negativeMax = array[0];
max = negativeMax;
} else {
positiveMax = array[0];
max = positiveMax;
}
for (int i = 1; i < array.length; i++) {
if (array[i] == 0) {
negativeMax = 0;
positiveMax = 0;
if (max < 0) {
max = 0;
}
} else if (array[i] > 0) {
if (positiveMax == 0) {
positiveMax = array[i];
} else {
positiveMax *= array[i];
}
if (negativeMax != 0) {
negativeMax *= array[i];
}
if (positiveMax > max) {
max = positiveMax;
}
} else {
if (array[i] > max) {
max = array[i];
}
if (negativeMax == 0) {
if (positiveMax != 0) {
negativeMax *= positiveMax;
} else {
negativeMax = array[i];
}
positiveMax = 0;
} else {
if (positiveMax != 0) {
int temp = positiveMax;
positiveMax = negativeMax * array[i];
negativeMax = temp * array[i];
} else {
positiveMax = negativeMax * array[i];
negativeMax = array[i];
}
if (positiveMax > max) {
max = positiveMax;
}
}
}
}
return max;
}
}
public class MaximumContiguousSubArrayProduct {
public static int getMaximumContiguousSubArrayProduct(final int... array) {
if (array.length == 0) {
return -1;
}
int negativeMax = 0, positiveMax = 0, max;
if (array[0] < 0) {
negativeMax = array[0];
max = negativeMax;
} else {
positiveMax = array[0];
max = positiveMax;
}
for (int i = 1; i < array.length; i++) {
if (array[i] == 0) {
negativeMax = 0;
positiveMax = 0;
if (max < 0) {
max = 0;
}
} else if (array[i] > 0) {
if (positiveMax == 0) {
positiveMax = array[i];
} else {
positiveMax *= array[i];
}
if (negativeMax != 0) {
negativeMax *= array[i];
}
if (positiveMax > max) {
max = positiveMax;
}
} else {
if (array[i] > max) {
max = array[i];
}
if (negativeMax == 0) {
if (positiveMax != 0) {
negativeMax *= positiveMax;
} else {
negativeMax = array[i];
}
positiveMax = 0;
} else {
if (positiveMax != 0) {
int temp = positiveMax;
positiveMax = negativeMax * array[i];
negativeMax = temp * array[i];
} else {
positiveMax = negativeMax * array[i];
negativeMax = array[i];
}
if (positiveMax > max) {
max = positiveMax;
}
}
}
}
return max;
}
}
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
public class MaximumContiguousSubArrayProductTest {
@Test
public void testMaximumProductSubArray() {
assertThat(MaximumContiguousSubArrayProduct.getMaximumContiguousSubArrayProduct(2, 3, -2, 4), equalTo(6));
assertThat(MaximumContiguousSubArrayProduct.getMaximumContiguousSubArrayProduct(2, 3, -2, 4, 9), equalTo(36));
assertThat(MaximumContiguousSubArrayProduct.getMaximumContiguousSubArrayProduct(-2, 0, -1), equalTo(0));
assertThat(MaximumContiguousSubArrayProduct.getMaximumContiguousSubArrayProduct(), equalTo(-1));
assertThat(MaximumContiguousSubArrayProduct.getMaximumContiguousSubArrayProduct(-1), equalTo(-1));
assertThat(MaximumContiguousSubArrayProduct.getMaximumContiguousSubArrayProduct(1), equalTo(1));
assertThat(MaximumContiguousSubArrayProduct.getMaximumContiguousSubArrayProduct(-9, -3, -4, -1), equalTo(9 * 3 * 4));
assertThat(MaximumContiguousSubArrayProduct.getMaximumContiguousSubArrayProduct(-1, 2), equalTo(2));
assertThat(MaximumContiguousSubArrayProduct.getMaximumContiguousSubArrayProduct(-100, -1, 99), equalTo(9900));
}
}