Maximum Product Subarray LeetCode solution in Python
1class Solution:2def maxProduct(self, nums: List[int]) -> int:3prod = 14maxprod = -1000000005for i in range(len(nums)): # traverse from L-R so that we get max6prod *= nums[i]7maxprod = max(maxprod, prod)8if prod == 0:9prod = 11011prod = 112for i in range(13len(nums) - 1, -1, -114): # if 0 or -ve present at starting then find from back15prod *= nums[i]16maxprod = max(maxprod, prod)17if prod == 0:18prod = 11920return maxprod