We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 766d0b6 + ecb5740 commit 31c676bCopy full SHA for 31c676b
solution/0152.Maximum Product Subarray/Solution.py
@@ -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