Skip to content

Commit db72d18

Browse files
committed
maximum-product-subarray sol (py)
1 parent 77aa070 commit db72d18

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
TC: O(n)
3+
SC: O(1)
4+
"""
5+
6+
class Solution:
7+
def maxProduct(self, nums: List[int]) -> int:
8+
max_here = min_here = ans = nums[0]
9+
10+
for x in nums[1:]:
11+
if x < 0:
12+
max_here, min_here = min_here, max_here
13+
max_here = max(x, max_here * x)
14+
min_here = min(x, min_here * x)
15+
ans = max(ans, max_here)
16+
return ans

0 commit comments

Comments
 (0)