package leetcode;

/**
 * https://leetcode.com/problems/maximum-product-subarray/
 */
public class Problem152 {
    public int maxProduct(int[] nums) {
        int answer = nums[0];
        int product = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (product * nums[i] == 0) {
                product = nums[i];
            } else {
                product *= nums[i];
            }
            int max = Math.max(nums[i], product);
            answer = Math.max(answer, max);
        }
        product = nums[nums.length - 1];
        for (int i = nums.length - 2; i >= 0; i--) {
            if (product * nums[i] == 0) {
                product = nums[i];
            } else {
                product *= nums[i];
            }
            int max = Math.max(nums[i], product);
            answer = Math.max(answer, max);
        }
        return answer;
    }
}

package leetcode;

/**
 * https://leetcode.com/problems/maximum-product-subarray/
 */
public class Problem152 {
    public int maxProduct(int[] nums) {
        int answer = nums[0];
        int min = nums[0];
        int max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int tmp = Math.max(nums[i], Math.max(nums[i] * min, nums[i] * max));
            min = Math.min(nums[i], Math.min(nums[i] * min, nums[i] * max));
            max = tmp;
            answer = Math.max(answer, max);
        }
        return answer;
    }
}

Update problem 152