Find the contiguous subarray within an array (containing at least one number) which has the largest product.
public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int[] max = new int[nums.length];
int[] min = new int[nums.length];
max[0] = nums[0];
min[0] = nums[0];
int res = nums[0];
for (int i = 1; i < nums.length; i++) {
max[i] = nums[i];
min[i] = nums[i];
if (nums[i] > 0) {
max[i] = Math.max(max[i], max[i - 1] * nums[i]);
min[i] = Math.min(min[i], min[i - 1] * nums[i]);
} else if (nums[i] < 0){
max[i] = Math.max(max[i], min[i - 1] * nums[i]);
min[i] = Math.min(min[i], max[i - 1] * nums[i]);
}
res = Math.max(res, max[i]);
}
return res;
}