Maximum product subarray

August 10, 2022

dynamic-programming

Problem URL: Maximum product subarray

We will keep track of minimum and maximum products of each item and keep the current maximum is our result. When the whole traversal is done, we will return the maximum result.

class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        res = max(nums)
        curMin, curMax = 1, 1
        for n in nums:
            temp = n*curMax
            curMax = max(n*curMax, n*curMin, n)
            curMin = min(temp, n*curMin, n)
            res = max(res, curMax)
        return res

Time Complexity: O(n)
Space Complexity: O(n)