Maximum Subarray Min-Product - Problem
Maximum Subarray Min-Product
You're given an array of integers and need to find the maximum min-product among all possible non-empty subarrays.
The min-product of a subarray is calculated as:
For example, in subarray
• Minimum value:
• Sum:
• Min-product:
Your goal is to find the subarray that gives the highest min-product value. Since the result can be very large, return it modulo
Key insight: You want to find the optimal balance between having a large minimum value and a large sum in your subarray.
You're given an array of integers and need to find the maximum min-product among all possible non-empty subarrays.
The min-product of a subarray is calculated as:
minimum_value × sum_of_all_elementsFor example, in subarray
[3, 2, 5]:• Minimum value:
2• Sum:
3 + 2 + 5 = 10• Min-product:
2 × 10 = 20Your goal is to find the subarray that gives the highest min-product value. Since the result can be very large, return it modulo
109 + 7.Key insight: You want to find the optimal balance between having a large minimum value and a large sum in your subarray.
Input & Output
example_1.py — Basic case
$
Input:
[1,2,3,2]
›
Output:
14
💡 Note:
The maximum min-product is achieved by subarray [2,3,2] with minimum=2 and sum=7, giving min-product=2×7=14
example_2.py — Single element optimal
$
Input:
[2,3,3,1,2]
›
Output:
18
💡 Note:
The maximum min-product is achieved by subarray [3,3] with minimum=3 and sum=6, giving min-product=3×6=18
example_3.py — Entire array optimal
$
Input:
[3,1,5,6,4,2]
›
Output:
60
💡 Note:
The maximum min-product is achieved by subarray [5,6] with minimum=5 and sum=11, giving min-product=5×11=55. Actually, it's [5,6,4] with min=4, sum=15, product=60
Visualization
Tap to expand
Understanding the Visualization
1
Identify Materials
Each array element represents a building material with a certain strength value
2
Find Optimal Sections
For each material, find the longest section where it's the weakest (determines project strength)
3
Calculate Profits
Profit = material_strength × sum_of_all_materials_in_section
4
Choose Maximum
Select the section that gives the highest profit
Key Takeaway
🎯 Key Insight: Each element can be the minimum in exactly one optimal subarray. Use monotonic stack to efficiently find the boundaries where each element is the bottleneck, maximizing the balance between minimum value and subarray sum.
Time & Space Complexity
Time Complexity
O(n)
Each element is pushed and popped from stack at most once, prefix sum is O(n)
✓ Linear Growth
Space Complexity
O(n)
Space for prefix sum array and monotonic stack
⚡ Linearithmic Space
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 106
- Return answer modulo 109 + 7
- All array elements are positive integers
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code