AC beat 89%, create a ResultType save maxProduct, minProduct


  • 0
    N

    ''''

        public class Solution {
        public int maxProduct(int[] nums) {
          if ( nums == null || nums.length == 0)
                            return 0;
        
        int n = nums.length;
        ResultType[] list = new ResultType[nums.length+1];
        for(int i = 0; i <list.length; i++){
            list[i] = new ResultType(1,1);
        }
    
        int max = Integer.MIN_VALUE;
        for (int i = 1; i <= nums.length; i++){
           list[i].maxProduct = Math.max(Math.max(list[i-1].maxProduct * nums[i-1], list[i-1].minProduct*nums[i-1]), nums[i-1]);
           list[i].minProduct = Math.min(Math.min(list[i-1].maxProduct * nums[i-1], list[i-1].minProduct*nums[i-1]), nums[i-1]);
           
           max = Math.max(max , list[i].maxProduct);
        }
        
        return max;
    }
    
    class ResultType{
        int maxProduct = 0;
        int minProduct = 0;
            public ResultType(int maxProduct, int minProduct){
                this.maxProduct = maxProduct;
                this.minProduct = minProduct;
            }
    }
    

    }


Log in to reply
 

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