-
Notifications
You must be signed in to change notification settings - Fork 1
/
maximum-product-subarray.py
39 lines (36 loc) · 1.03 KB
/
maximum-product-subarray.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
# coding=utf-8
"""
152. Maximum Product Subarray
"""
class Solution(object):
def maxProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return
max_product = nums[0]
max_pre = min_pre = nums[0]
n = len(nums)
for i in range(1, n):
max_dpi = max(nums[i] * max_pre, nums[i], nums[i] * min_pre)
min_dpi = min(nums[i] * max_pre, nums[i], nums[i] * min_pre)
max_pre = max_dpi
min_pre = min_dpi
max_product = max(max_product, max_dpi)
return max_product
def maxProduct1(self, nums):
"""
从discuss看到的,简明写法
:type nums: List[int]
:rtype: int
"""
if not nums:
return
max_product = nums[0]
big = small = nums[0]
for i in nums[1:]:
big, small = max(i, i*big, i*small), min(i, i*big, i*small)
max_product = max(big, max_product)
return max_product