O(n) time, O(1) space JS solution


  • 0
    var maxProduct = function (nums) {
        let f = 1, b = 1, res = nums[0];
        for (let i = 0; i < nums.length; i++) {
            const j = nums.length - 1 - i;
            f = f * nums[i] || nums[i];
            b = b * nums[j] || nums[j];
            res = Math.max(res, f, b);
        }
        return res;
    };
    

Log in to reply
 

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