Skip to content

Commit fdcf209

Browse files
committed
maximum-product-subarray solution
1 parent 8d3d967 commit fdcf209

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
# time O(n), space O(1)
4+
class Solution:
5+
def maxProduct(self, nums: List[int]) -> int:
6+
max_prod = nums[0]
7+
min_prod = nums[0]
8+
result = nums[0]
9+
10+
for i in range(1, len(nums)):
11+
candidates = (nums[i], nums[i] * max_prod, nums[i] * min_prod)
12+
max_prod, min_prod = max(candidates), min(candidates)
13+
result = max(result, max_prod)
14+
15+
return result
16+
17+
18+
19+

0 commit comments

Comments
 (0)