프로그래밍/LeetCode
[LeetCode][Python3] 152. Maximum Product Subarray
snoopybox
2019. 4. 22. 23:39
Problem :
https://leetcode.com/problems/maximum-product-subarray/
My Solution :
class Solution:
def maxProduct(self, nums: List[int]) -> int:
ans = pos = neg = nums[0]
for n in nums[1:]:
if n < 0:
pos, neg = n*neg, min(n*pos, n)
else:
pos, neg = max(n*pos, n), n*neg
ans = max(ans, pos)
return ans