题解 | #连续子数组的最大乘积#
连续子数组的最大乘积
http://www.nowcoder.com/practice/fd8c819c07c9493887bfac8549c119f4
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
int[] dpMax = new int[n];
int[] dpMin = new int[n];
dpMax[0] = nums[0];
dpMin[0] = nums[0];
for (int i = 1; i < n; i++) {
dpMax[i] = Math.max(dpMax[i - 1] * nums[i], Math.max(dpMin[i - 1] * nums[i], nums[i]));
dpMin[i] = Math.min(dpMax[i - 1] * nums[i], Math.min(dpMin[i - 1] * nums[i], nums[i]));
}
int res = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (dpMax[i] > res) res = dpMax[i];
}
System.out.println(res);
}
}