Skip to content

Commit 31c676b

Browse files
authored
Merge pull request #210 from kirisi2008/dev
add python solution for 0152
2 parents 766d0b6 + ecb5740 commit 31c676b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
# Beats 88% in python.
3+
class Solution(object):
4+
def maxProduct(self, nums):
5+
"""
6+
:type nums: List[int]
7+
:rtype: int
8+
"""
9+
if not nums:
10+
return 0
11+
localMin = localMax = maxi = nums[0]
12+
13+
for i in range(1, len(nums)):
14+
if nums[i] < 0:
15+
localMin, localMax = localMax, localMin
16+
17+
localMin = min(nums[i], localMin * nums[i])
18+
localMax = max(nums[i], localMax * nums[i])
19+
maxi = max(localMax, maxi)
20+
21+
return maxi

0 commit comments

Comments
 (0)