DP + 滚动矩阵
public class Solution {
public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return -1;
}
int result = nums[0];
int max = nums[0];
int min = nums[0];
int maxTemp = max;
int minTemp = min;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > 0) {
max = Math.max(nums[i], maxTemp * nums[i]);
min = Math.min(nums[i], minTemp * nums[i]);
} else if (nums[i] < 0) {
max = Math.max(nums[i], minTemp * nums[i]);
min = Math.min(nums[i], maxTemp * nums[i]);
} else {
max = 0;
min = 0;
}
result = Math.max(result, max);
maxTemp = max;
minTemp = min;
}
return result;
}
}