<span style="white-space:pre"> </span>public int maxProduct(int[] A, int start, int end) {
int product = 1;
int leftProduct = 1;
for(int i = start; i <= end; i++)
{
product *= A[i];
}
int max = product;
for(int i = start; i < end; i++)
{
leftProduct *=A[i];
int rightProduct = product/leftProduct;
max = Math.max(leftProduct, max);
max = Math.max(rightProduct, max);
}
return max;
}
public int maxProduct(int A[])
{
int max = Integer.MIN_VALUE;
int start = 0;
for(int i = 0; i < A.length; i++)
{
if(A[i] == 0)
{
if(start <= i-1)
{
max = Math.max(max, maxProduct(A, start, i-1));
}
max = Math.max(0, max);
start = i+1;
}
}
if(start < A.length)
max = Math.max(max, maxProduct(A, start, A.length-1));
return max;
}
[leetcode] Maximum Product Subarray
最新推荐文章于 2024-06-05 14:36:41 发布