Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def maxProduct(self, nums: List[int]) -> int:
- ans=float('-inf')
- pos,neg,ans=1,1,max(nums)
- for i in nums:
- temp=pos
- pos=max(pos*i,neg*i,i)
- '''
- For e.g. [-1,8] To avoid -8 as final answer we use third parameter.
- So if array starts with negative number this third parameter helps out
- '''
- neg=min(temp*i,neg*i,i)
- '''
- Here we are including third parameter.
- If we dont include third parameter,
- for e.g. if array contains all positive numbers error occurs.
- '''
- ans=max(pos,ans)#Here update max with pos and ans .(VVVimp)
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement