-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_prd_subarr.py
51 lines (44 loc) · 1.05 KB
/
max_prd_subarr.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
def my_maxProduct(nums): #!My wrong method...FAIL
"""
:type nums: List[int]
:rtype: int
"""
mx = -999999
currM = 1
for i in range(len(nums)):
currM*=nums[i]
mx = max(mx, currM)
if currM <= 0:
if currM == 0:
mx = 0
currM = 1
continue
elif i+1 < len(nums):
if nums[i+1] < 0:
continue
currM = 1
return(mx)
def maxProduct(nums): #!NEETCODE
"""
:type nums: List[int]
:rtype: int
"""
op = max(nums)
currMax = 1
currMin = 1
for n in nums:
if n == 0:
currMax = 1
currMin = 1
continue
tmp = currMax * n
currMax = max(tmp, currMin*n, n) #comparing it to n is a very imp point
currMin = min(tmp, currMin*n, n)
op = max(op,currMax, currMin)
return(op)
def main():
nums = [-3,-1,-1]
op = maxProduct(nums)
print(op)
if __name__ == "__main__":
main()