10 lines Java DP solution


  • 0
        public int maxProduct(int[] nums) {
            if(nums.length == 0) return 0;
            int[] maxs = new int[nums.length];
            int[] mins = new int[nums.length];
            maxs[0] = mins[0] = nums[0];
            for(int i=1;i<nums.length;i++){
                int[] n3 = new int[]{nums[i], nums[i]*maxs[i-1], nums[i]*mins[i-1]};
                maxs[i] = IntStream.of(n3).max().getAsInt();
                mins[i] = IntStream.of(n3).min().getAsInt();
            }
            
            return IntStream.of(maxs).max().getAsInt();
        }
    ···

Log in to reply
 

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