152. Maximum Product Subarray

202401170957
tags: #dp

class Solution(object):
	def maxProduct(self, nums):
		res = lmax = lmin = nums[0]
		for i in range(1, len(nums)):
			if nums[i] < 0:
				lmax, lmin = lmin, lmax
			lmax = max(nums[i], lmax * nums[i])
			lmin = min(nums[i], lmin * nums[i])
			res = max(res, lmax)
		return res

Reference

https://leetcode.com/problems/maximum-product-subarray/