To solve the problem of finding the maximum min-product of any non-empty subarray of an array nums, we'll use a stack-based approach combined with prefix sums. This method efficiently computes the required min-products by identifying the boundaries where each element can be the minimum and then calculating the sum of the corresponding subarray.
Here's a step-by-step breakdown of the approach:
-
Prefix Sum Calculation:
- Compute the prefix sums of the array to allow quick calculation of the sum of any subarray. The prefix sum at index
i represents the sum of elements from the start of the array up to (but not including) index i.
-
Finding Boundaries Using Stacks:
- For each element in the array, determine the first smaller element to its left and the first smaller element to its right. These boundaries define the range where the current element is the minimum.
- Use two separate stacks to find these boundaries efficiently:
- Left Boundaries: Find the index of the first smaller element to the left of each element.
- Right Boundaries: Find the index of the first smaller element to the right of each element.
-
Calculating Min-Product:
- For each element, using its left and right boundaries, calculate the sum of the subarray where this element is the minimum using the prefix sums.
- The min-product for this subarray is then the element value multiplied by the subarray sum.
- Keep track of the maximum min-product encountered during this process.
-
Modulo Operation:
- Since the result can be very large, return the maximum min-product modulo (10^9 + 7) as specified.
Here's the Python implementation of the above approach:
def max_min_product(nums):
MOD = 10**9 + 7
n = len(nums)
prefix_sum = [0] * (n +1)
for i in range(n):
prefix_sum[i+1] = prefix_sum[i] + nums[i]
# Initialize stacks for finding previous and next less elements
stack = []
left = [ -1 ] * n # Previous Less Element indices
for i in range(n):
while stack and nums[stack[-1]] >= nums[i]:
stack.pop()
left[i] = stack[-1] if stack else -1
stack.append(i)
stack = []
right = [n] * n # Next Less Element indices
for i in range(n-1, -1, -1):
while stack and nums[stack[-1]] >= nums[i]:
stack.pop()
right[i] = stack[-1] if stack else n
stack.append(i)
max_product = 0
for i in range(n):
total = prefix_sum[right[i]] - prefix_sum[left[i]+1]
product = nums[i] * total
max_product = max(max_product, product)
return max_product % MOD
# Example usage:
nums = [3, 2, 5]
print(max_min_product(nums)) # Output: 25
Explanation of the Code:
-
Prefix Sum:
- We create a
prefix_sum array where prefix_sum[i] represents the sum of elements from nums[0] to nums[i-1].
- This allows us to calculate the sum of any subarray
nums[l..r] as prefix_sum[r+1] - prefix_sum[l].
-
Finding Previous Less Elements (left):
- We iterate through the array from left to right.
- For each element
nums[i], we pop elements from the stack until we find an element smaller than nums[i].
- The index of this smaller element is stored in
left[i]. If no such element exists, left[i] remains -1.
-
Finding Next Less Elements (right):
- We iterate through the array from right to left.
- For each element
nums[i], we pop elements from the stack until we find an element smaller than nums[i].
- The index of this smaller element is stored in
right[i]. If no such element exists, right[i] remains n (the length of the array).
-
Calculating Max Min-Product:
- For each element
nums[i], the subarray where nums[i] is the minimum extends from left[i] + 1 to right[i] - 1.
- The sum of this subarray is calculated using the
prefix_sum array.
- The min-product is then
nums[i] * sum_of_subarray.
- We keep track of the maximum min-product encountered.
-
Returning the Result:
- Finally, we return the maximum min-product modulo (10^9 + 7) as required.
Example Walkthrough:
Consider the array [3, 2, 5]:
- Prefix Sum:
[0, 3, 5, 10]
- Left Boundaries:
- For
3, no smaller element to the left: left[0] = -1
- For
2, no smaller element to the left: left[1] = -1
- For
5, the first smaller element to the left is 2 at index 1: left[2] = 1
- Right Boundaries:
- For
3, the first smaller element to the right is 2 at index 1: right[0] = 1
- For
2, no smaller element to the right: right[1] = 3
- For
5, no smaller element to the right: right[2] = 3
- Calculating Min-Products:
- For
3: subarray [3], sum = 3, min-product = 3 * 3 = 9
- For
2: subarray [3, 2, 5], sum = 10, min-product = 2 * 10 = 20
- For
5: subarray [5], sum = 5, min-product = 5 * 5 = 25
- Maximum Min-Product:
25
Thus, the function correctly returns 25.
Complexity Analysis:
- Time Complexity: (O(n)), where (n) is the length of the
nums array. Each element is pushed and popped from the stack at most once.
- Space Complexity: (O(n)) for the prefix sum array and the auxiliary stacks.