public class Solution {
public int maxProduct(int[] nums) {
int res = Integer.MIN_VALUE;
if (nums == null || nums.length < 1) return res;
int len = nums.length;
int[][] dp = new int[len][2];
dp[0][0] = nums[0];
dp[0][1] = nums[0];
res = nums[0];
for (int i = 1; i < len; i++) {
int first = dp[i - 1][0] * nums[i];
int second = dp[i - 1][1] * nums[i];
dp[i][0] = Math.min(Math.min(first, second), nums[i]);
dp[i][1] = Math.max(Math.max(first, second), nums[i]);
res = Math.max(res, dp[i][1]);
}
return res;
}
}
public class Solution {
public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int n = nums.length;
int[] min = new int[n];
int[] max = new int[n];
min[0] = nums[0];
max[0] = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] > 0) {
max[i] = Math.max(nums[i], max[i - 1] * nums[i]);
min[i] = Math.min(nums[i], min[i - 1] * nums[i]);
} else {
max[i] = Math.max(nums[i], min[i - 1] * nums[i]);
min[i] = Math.min(nums[i], max[i - 1] * nums[i]);
}
}
int result = Integer.MIN_VALUE;
for (int num : max) {
result = Math.max(result, num);
}
return result;
}
}