Skip to content

Commit a176505

Browse files
authored
Create maximum-subarray-min-product.py
1 parent 9ea331b commit a176505

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+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def maxSumMinProduct(self, nums):
6+
"""
7+
:type nums: List[int]
8+
:rtype: int
9+
"""
10+
MOD = 10**9+7
11+
prefix = [0]*(len(nums)+1)
12+
for i in xrange(len(nums)):
13+
prefix[i+1] = prefix[i]+nums[i]
14+
stk, result = [-1], 0
15+
for i in xrange(len(nums)+1):
16+
while stk[-1] != -1 and (i == len(nums) or nums[stk[-1]] >= nums[i]):
17+
result = max(result, nums[stk.pop()]*(prefix[(i-1)+1]-prefix[stk[-1]+1]))
18+
stk.append(i)
19+
return result%MOD

0 commit comments

Comments
 (0)