152. Maximum Product Subarray
Medium
Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.Input: nums = [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.Last updated
Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.Input: nums = [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.Last updated
class Solution:
def maxProduct(self, nums: List[int]) -> int:
maximum = nums[0]
minimum = nums[0]
max_of_all = nums[0]
nums.pop(0)
for num in nums:
temp = max(num, num*maximum, num*minimum)
minimum = min(num , num*maximum, num*minimum)
maximum = temp
max_of_all = max(max_of_all, maximum)
return max_of_all