The solutions for the input [-2,0,-1]


  • 0
    J

    Here is my code for the solution, but I have no clue how to solve the input [-2,0,-1]

     public static int getMax(int x,int y){
    	return x>y?x:y;
    }
    public static int getMin(int x,int y){
    	return x<y?x:y;
    }
    public int maxProduct(int[] A) {
         int max_product=0;
    	int max_value=1;
    	int min_value=1;
    		
    	for(int i=0;i<A.length;i++){
    	    if(A.length==1){
    			return max_product=A[0];
    		}
    	
    		if(A[i]>0){
    			max_value=max_value*A[i];
    			min_value=getMin(min_value*A[i],1);
    		}
    		else if(A[i]==0){
    			max_value=1;
    			min_value=1;
    		}
    		else{
    			int tmp=max_value;
    			max_value=getMax(min_value*A[i],1);
    			min_value=tmp*A[i];
    			
    		}
    		if(max_product<max_value){
    			max_product=max_value;
    		}
    			
    	}
    	
    	return max_product;
    }

Log in to reply
 

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