The AC code ,is this DP?


  • 0
    M
    public class Solution { 
         public static int maxProduct(int[] A) {
    
           	 int len = A.length;
    	 int Max=A[0];
    	 int[] dp2 = new int[len];
    	 dp2[0]=A[0];
    	 int start=0;
    	 boolean flag=false;
    	 int tag=0;
    	 if(A[0]<0){
    		 flag=true;
    	 }
    	 for(int i=1;i<len;i++){
    		 if(dp2[i-1]==0){
    			 flag=false;
    			 dp2[i]=A[i];
    			 start=i;
    			 tag=i;
    			 if(A[i]<0&&!flag){
    			     flag=true;
    			 }
    		 }else{
    		     if(!flag&&A[i]<0){
    		         flag=true;
    		         tag=i;
    		     }
    			 dp2[i]=dp2[i-1]*A[i];
    			 if(dp2[i]<0){
    			     int res=1;
    			     if(start==tag){
    			    	 res*=A[start];
    			     }else {
    			    	 for(int j=start;j<=tag;j++){
    					     res*=A[j];
    					 }
    				}
    				 
    				 Max=Math.max(Max,dp2[i]/res);
    			 }
    		 }
    		 Max=Math.max(dp2[i], Max);
    	 }
    	 return Max;
     }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.