11. Sum of Subarray Minimums
β GFG solution to the Sum of Subarray Minimums problem: calculate the sum of minimum elements across all subarrays using monotonic stack technique. π
π§© Problem Description
π Examples
Example 1
Input: arr[] = [10, 20]
Output: 40
Explanation: Subarrays are [10], [20], [10, 20].
Minimums are 10, 20, 10.
Sum of all these is 10 + 20 + 10 = 40.Example 2
Input: arr[] = [1, 2, 3, 4]
Output: 20
Explanation: Subarrays are [1], [2], [3], [4], [1, 2], [1, 2, 3], [1, 2, 3, 4],
[2, 3], [2, 3, 4], [3, 4].
Minimums are 1, 2, 3, 4, 1, 1, 1, 2, 2, 3.
Sum of all these is 1 + 2 + 3 + 4 + 1 + 1 + 1 + 2 + 2 + 3 = 20.π Constraints
β
My Approach
Two-Boundary Monotonic Stack
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated