-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path787-Maximum Sub_Sequence Product.py
66 lines (52 loc) · 1.48 KB
/
787-Maximum Sub_Sequence Product.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
def MaxProduct(A):
curr_max = A[0]
prev_max = A[0]
prev_min = A[0]
ans = A[0]
for i in range(1,len(A)):
curr_max = max(prev_max*A[i], A[i])
curr_min = min(prev_min*A[i], A[i])
ans = max(ans, curr_max)
# prev_max = curr_max
prev_max = curr_min
return ans
def MaxProduct2(A):
max_end = A[0]
min_end = A[0]
res = A[0]
for i in range(1,len(A)):
if A[i] > 0:
max_end = max_end*A[i]
min_end = min(min_end*A[i],A[i])
else:
temp = max_end
max_end = max(min_end*A[i], A[i])
min_end = temp*A[i]
if res < max_end:
res = max_end
return res
# def MAXPRODUCT(array):
# curr_max_prod = array[0]
# prev_max_prod = array[0]
# prev_min_prod = array[0]
#
# ans = array[0]
#
# for i in range(1,len(array)):
# curr_max_prod = max(max(prev_max_prod*array[i], prev_max_prod*array[i]),array[i])
# curr_min_prod = min(min(prev_max_prod*array[i],prev_min_prod*array[i]),array[i])
# ans = max(ans,curr_max_prod)
# prev_max_prod = curr_max_prod
# prev_max_prod = curr_min_prod
#
# return ans
while True:
#sys.stdin = open('Max_Prod.txt')
try:
A = list(map(int, input().split()))
A = A[:-1]
# print(MAXPRODUCT(A))
print(MaxProduct2(A))
except EOFError:
break