C# - from the left, then from the right


  • 0
    public int MaxProduct(int[] nums) 
    {
        int max = nums[0];
        
        // iterate from left
        int curr = 1;
        for (int i = 0; i < nums.Length; i++)
        {
            curr *= nums[i];
            if (curr > max) max = curr;
            if (curr == 0) curr = 1;
        }
        
        // iterate from right
        curr = 1;
        for (int i = nums.Length - 1; i >= 0; i--)
        {
            curr *= nums[i];
            if (curr > max) max = curr;
            if (curr == 0) curr = 1;
        }
        
        return max;
    }

Log in to reply
 

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