Java Solution for Maximum Product SubArray


  • 0
    A

    class Solution {
    public int maxProduct(int[] nums) {

        int dp[][] = new int[2][nums.length];
        
        dp[0][0] = nums[0];
        dp[1][0] = nums[0];
        
        int max = dp[0][0];
        
        for(int i =1 ;i< nums.length;i++){
            
    
            dp[0][i] = Math.max(Math.max(nums[i], nums[i]*dp[0][i-1]), nums[i]* dp[1][i-1]);
            dp[1][i] = Math.min(Math.min(nums[i], nums[i]*dp[0][i-1]), nums[i]* dp[1][i-1]);            
            max = Math.max(max, dp[0][i] );                        
        }
        
        return max;
    }
    

    }


Log in to reply
 

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