152. Maximum Product Subarray
https://leetcode.com/problems/maximum-product-subarray
Python
class Solution:
def maxProduct(self, nums: List[int]) -> int:
if not nums:
return
ans_product = nums[0]
current_max = nums[0]
current_min = nums[0]
for num in nums[1:]:
if num < 0:
current_max, current_min = current_min, current_max
current_max = max(current_max * num, num)
current_min = min(current_min * num, num)
ans_product = max(current_max, ans_product)
return ans_product