package com.fishercoder.solutions;

/**
 * Find the contiguous subarray within an array (containing at least one number) which has the largest product.

 For example, given the array [2,3,-2,4],
 the contiguous subarray [2,3] has the largest product = 6.
 */
public class _152 {

    public int maxProduct(int[] nums) {
        int pos = nums[0];
        int neg = nums[0];
        int max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int temp = pos;
            pos = Math.max(nums[i], nums[i] * ((nums[i] >= 0) ? pos : neg));
            neg = Math.min(nums[i], nums[i] * ((nums[i] >= 0) ? neg : temp));
            max = Math.max(max, pos);
        }
        return max;
    }

}

@@ -35,16 +35,48 @@
 %0A%0A/**%0A *
+ 152. Maximum Product Subarray%0A%0A
  Find th
@@ -177,16 +177,16 @@
 roduct.%0A
-
 %0A For ex
@@ -304,17 +304,50 @@
  _152 %7B%0A
+  public static class Solution1 %7B
 %0A
-
     publ
@@ -380,26 +380,24 @@
 ms) %7B%0A      
-  
 int pos = nu
@@ -395,34 +395,32 @@
  pos = nums%5B0%5D;%0A
-  
       int neg = 
@@ -430,26 +430,24 @@
 s%5B0%5D;%0A      
-  
 int max = nu
@@ -459,18 +459,16 @@
 ;%0A      
- 
- 
 for (int
@@ -507,20 +507,16 @@
         
-    
 int temp
@@ -531,20 +531,16 @@
         
-  
-  
 pos = Ma
@@ -592,28 +592,24 @@
 os : neg));%0A
-    
         neg 
@@ -678,20 +678,16 @@
         
- 
-   
 max = Ma
@@ -714,14 +714,10 @@
     
-  %7D%0A  
+%7D%0A
     
@@ -736,11 +736,14 @@
 ;%0A    %7D%0A
+  %7D
 %0A%7D%0A

refactor 152