2Brute2Best
🔥0

Sum of Subarray Minimums

Medium·LeetCode #907 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given an integer array arr.

Look at every contiguous subarray of arr. Take the minimum of each one and add all those minimums together. The total can be huge, so return it modulo 10^9 + 7.

Input

An integer array arr.

Output

The sum of min(subarray) over all subarrays, modulo 10^9 + 7.

Example 1
Input: arr = [3, 1, 2, 4]
Output: 17

The subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. Their minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1, which add up to 17.

Example 2
Input: arr = [11, 81, 94, 43, 3]
Output: 444
Constraints
  • • 1 ≤ arr.length ≤ 3 · 10^4
  • • 1 ≤ arr[i] ≤ 3 · 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“sum over all subarrays”
→ n²/2 subarrays is too many to visit → count each element's share instead.
“minimum of each subarray”
→ An element is the min until a smaller one appears → previous smaller / next smaller walls.
“modulo 10^9 + 7”
→ The total is big → long long for the products, then mod.
03

Approach Complexity Ladder

Brute Force — every subarray with a running minO(N²)O(1)

Fix the start, extend the end, add the running minimum each time

Optimal — contribution of each elementO(N)O(N)

arr[i] is the min of left[i] × right[i] subarrays; find both counts with a monotonic stack

Brute Force Intuition

Fix the start of the subarray. Move the end one step at a time and keep the running minimum. Each (start, end) pair is one subarray, so add the current minimum to the total.

Recognition rule for this sub-pattern:

For each element, find how far it stretches left and right as the min (or max). Histograms: area = height × width. Sums over subarrays: value × (left span) × (right span), with one side strict and the other ≤ so ties count once.
Remember this · Monotonic Stack
Trigger
Next/previous greater or smaller element, spans, histogram areas, "for each element, how far does it dominate?"
Think
When a new element arrives, which older elements can never be the answer again?
Key question
Is my stack increasing or decreasing, and what do I record when I pop?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.