Maximum Product Subarray

 

152. Maximum Product Subarray

Solution

  1. Only when the num is 0, we are sure to restart.

  2. Choose the even number of negative nums from left and right, compare them.

def maxProduct(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    res = nums[0]
    curr = 1
    
    for num in nums:
        curr = curr * num
        res = max(res, curr)
        if curr == 0:
            curr = 1
            
    curr = 1
    for num in reversed(nums):
        curr = curr * num
        res = max(res, curr)
        if curr == 0:
            curr = 1
    return res