-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path152.py
40 lines (38 loc) · 1001 Bytes
/
152.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution:
def maxProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxP = nums[0]
minP = nums[0]
i = 1
res = nums[0]
while i < len(nums):
temp = maxP
maxP = max(nums[i], nums[i] * temp, nums[i] * minP)
minP = min(nums[i], nums[i] * temp, nums[i] * minP)
i += 1
res = max(maxP, res)
return res
##############
class Solution:
def maxProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_val = -float("inf")
product = 1
for num in nums:
product *= num
max_val = max(product, max_val)
if num == 0:
product = 1
product = 1
for num in nums[::-1]:
product *= num
max_val = max(product, max_val)
if num == 0:
product = 1
return max_val