Skip to content

Commit f407e6e

Browse files
committed
solve : maximum product subarray
1 parent bab86ff commit f407e6e

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+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def maxProduct(self, nums: List[int]) -> int:
5+
max_overall = nums[0]
6+
current_min_product = current_max_product = 1
7+
8+
for num in nums:
9+
temp_min = min(current_min_product * num, current_max_product * num, num)
10+
current_max_product = max(
11+
current_min_product * num, current_max_product * num, num
12+
)
13+
current_min_product = temp_min
14+
max_overall = max(current_max_product, max_overall)
15+
16+
return max_overall

0 commit comments

Comments
 (0)