We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents b02425a + e2b43bd commit 5960de9Copy full SHA for 5960de9
maximum-product-subarray/joon.py
@@ -0,0 +1,18 @@
1
+from typing import List
2
+
3
4
+class Solution:
5
+ # Time: O(n)
6
+ # Space: O(n)
7
+ def maxProduct(self, nums: List[int]) -> int:
8
+ maxProducts = [nums[0]]
9
+ minProducts = [nums[0]]
10
+ # Store all max/minProducts[i]: the max/min product of all subarrays that have nums[i] as the last element.
11
12
13
+ for num in nums[1:]:
14
+ newMaxProduct = max(maxProducts[-1] * num, minProducts[-1] * num, num)
15
+ newMinProduct = min(maxProducts[-1] * num, minProducts[-1] * num, num)
16
+ maxProducts.append(newMaxProduct)
17
+ minProducts.append(newMinProduct)
18
+ return max(maxProducts)
0 commit comments