prefixSum
with an additional 0 at the beginning for convenience.nums
to fill in the prefixSum
array.stack
to keep track of indices of elements.nums
and for each element:
a. While the stack is not empty and the current element is less than the element at the index on the top of the stack, pop from the stack.
b. Calculate the min-product using the current element as the minimum, with the sum of the subarray from the next smaller element on the left to the current index.
c. Update the maximum min-product if the current min-product is greater.