package leetcode;

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

@@ -157,19 +157,22 @@
     int 
-max
+answer
  = nums%5B
@@ -187,22 +187,23 @@
     int 
-tmpMax
+product
  = nums%5B
@@ -262,38 +262,39 @@
             if (
-tmpMax
+product
  * nums%5Bi%5D == 0)
@@ -304,38 +304,39 @@
                 
-tmpMax
+product
  = nums%5Bi%5D;%0A    
@@ -360,38 +360,39 @@
                 
-tmpMax
+product
  *= nums%5Bi%5D;%0A   
@@ -406,32 +406,36 @@
   %7D%0A            
+int 
 max = Math.max(M
@@ -437,33 +437,63 @@
 max(
-Math.max(tmpMax, nums%5Bi%5D)
+nums%5Bi%5D, product);%0A            answer = Math.max(answer
 , ma
@@ -506,38 +506,39 @@
       %7D%0A        
-tmpMax
+product
  = nums%5Bnums.len
@@ -616,22 +616,23 @@
     if (
-tmpMax
+product
  * nums%5B
@@ -658,22 +658,23 @@
         
-tmpMax
+product
  = nums%5B
@@ -714,22 +714,23 @@
         
-tmpMax
+product
  *= nums
@@ -760,16 +760,20 @@
         
+int 
 max = Ma
@@ -783,33 +783,63 @@
 max(
-Math.max(tmpMax, nums%5Bi%5D)
+nums%5Bi%5D, product);%0A            answer = Math.max(answer
 , ma
@@ -867,19 +867,22 @@
  return 
-max
+answer
 ;%0A    %7D%0A

Update problem 152