-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path152. Maximum Product Subarray.py
More file actions
52 lines (45 loc) · 1.12 KB
/
152. Maximum Product Subarray.py
File metadata and controls
52 lines (45 loc) · 1.12 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
class Solution(object):
def maxProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l_nums = len(nums)
products = 1
max_sum = -sys.maxint
for i in range(l_nums):
products *= nums[i]
if max_sum < products:
max_sum = products
if products == 0:
products = 1
products = 1
for i in range(l_nums):
products *= nums[l_nums - i - 1]
if max_sum < products:
max_sum = products
if products == 0:
products = 1
return max_sum
if __name__ == '__main__':
"""
Example 1:
Input: [2,3,-2,4]
Output: 6
Example 2:
Input: [-2,0,-1]
Output: 0
"""
t1 = [2, 3, -2, 4]
t2 = [-2, 0, -1]
t3 = [-4, -3]
t4 = [0, 2]
t5 = [3, -1, 4]
print(Solution().maxProduct(t1))
print(Solution().maxProduct(t2))
print(Solution().maxProduct(t3))
print(Solution().maxProduct(t4))
print(Solution().maxProduct(t5))