因为存在负数 所以需要维护一个最小乘积minDp
维护一个最大乘积maxDp
public double maxProduct(double[] arr) {
int len = arr.length;
if (arr == null || len == 0){
return (double) 0.0;
}
double[] maxDp = new double[len];
double[] minDp = new double[len];
System.arraycopy(arr,0,maxDp,0,len);
System.arraycopy(arr,0,minDp,0,len);
for (int i = 1; i < len; i++) {
double num = arr[i];
maxDp[i] = Math.max(maxDp[i-1]*num,Math.max(num,minDp[i-1]*num));
minDp[i] = Math.min(minDp[i-1]*num,Math.min(num,maxDp[i-1]*num));
}
double maxProduct = maxDp[0];
for (int i = 1; i < len; i++) {
maxProduct = Math.max(maxProduct,maxDp[i]);
}
return maxProduct;
}
}