Iam_Sandeep

1856. Maximum Subarray Min-Product

Jul 16th, 2022 (edited)
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. #same like histogram solution
  2. class Solution:
  3.     def maxSumMinProduct(self, nums: List[int]) -> int:
  4.         n=len(nums)
  5.         pre=[0]
  6.         for i in range(n):
  7.             pre.append(pre[-1]+nums[i])
  8.         q=deque()
  9.         ans=0
  10.         for i,j in enumerate(nums):
  11.             newStart=i
  12.             while q and q[-1][0]>j:
  13.                 item,popindex=q.pop()
  14.                 newStart=popindex
  15.                 total=pre[i]-pre[popindex]
  16.                 ans=max(ans,total*item)
  17.             q.append((j,newStart))
  18.         while q:
  19.             item,popindex=q.pop()
  20.             total=pre[-1]-pre[popindex]
  21.             ans=max(ans,total*item)
  22.         return ans
  23.                
Add Comment
Please, Sign In to add comment