O(n) time and O(1) space complexity ... 3ms ...


  • 0
    K

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

        int l=nums.length;
        
        if(l==0)
        return 0;
        
        int max= nums[0] ;
        int min= nums[0] ;
        
        int res = nums[0];
        
        for(int i =1 ; i<l ; i++)
        {
            int a = nums[i];
            int b = max*nums[i];
            int c = min*nums[i];
            
            min = Math.min(a,Math.min(b,c));
            max= Math.max(a,Math.max(b,c));
            
            res = Math.max(res,max);
            
        }
        
        
        return res;
        
        
    }
    

    }

    '''


Log in to reply
 

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