Skip to content
DSA Patterns
Menu
Language
Monotonic StackLC #9076 of 6← Previous

Demo preview: These solutions pass the automated test suite but have not been human-reviewed. Treat trade-offs and prose as draft.

Sum of Subarray Minimums

Problem (restated)

For every non-empty contiguous subarray, take its minimum; return the sum of those minima mod 10^9+7.

Intuition

arr[i] is min of left[i]*right[i] subarrays (strict on one side to handle ties). Monotonic stack finds span.

Approaches

Contribution via next smaller

Unverified
Time O(n)Space O(n)

Idea. left[i] = distance to the previous smaller-or-equal; right[i] = distance to the next strictly smaller. ans += arr[i]leftright.

Walkthrough. [3,1,2,4] → 17.

Trade-offs. Brute O(n^2). Asymmetric </≤ avoids double-counting equal mins.

Solution
export function sumSubarrayMins(arr: number[]): number {
  const MOD = 1_000_000_007;
  const n = arr.length;
  const left = Array(n).fill(0);
  const right = Array(n).fill(0);
  const stack: number[] = [];
  for (let i = 0; i < n; i++) {
    while (stack.length && arr[stack[stack.length - 1]!]! > arr[i]!) stack.pop();
    left[i] = stack.length === 0 ? i + 1 : i - stack[stack.length - 1]!;
    stack.push(i);
  }
  stack.length = 0;
  for (let i = n - 1; i >= 0; i--) {
    while (stack.length && arr[stack[stack.length - 1]!]! >= arr[i]!) stack.pop();
    right[i] = stack.length === 0 ? n - i : stack[stack.length - 1]! - i;
    stack.push(i);
  }
  let ans = 0;
  for (let i = 0; i < n; i++) ans = (ans + arr[i]! * left[i]! * right[i]!) % MOD;
  return ans;
}
export function sumSubarrayMins(arr: number[]): number {
  const MOD = 1_000_000_007;
  const n = arr.length;
  const left = Array(n).fill(0);
  const right = Array(n).fill(0);
  const stack: number[] = [];
  for (let i = 0; i < n; i++) {
    while (stack.length && arr[stack[stack.length - 1]!]! > arr[i]!) stack.pop();
    left[i] = stack.length === 0 ? i + 1 : i - stack[stack.length - 1]!;
    stack.push(i);
  }
  stack.length = 0;
  for (let i = n - 1; i >= 0; i--) {
    while (stack.length && arr[stack[stack.length - 1]!]! >= arr[i]!) stack.pop();
    right[i] = stack.length === 0 ? n - i : stack[stack.length - 1]! - i;
    stack.push(i);
  }
  let ans = 0;
  for (let i = 0; i < n; i++) ans = (ans + arr[i]! * left[i]! * right[i]!) % MOD;
  return ans;
}

Template connection

left[i] is the distance to the previous smaller-or-equal value, and right[i] is the distance to the next strictly smaller value. arr[i] * left[i] * right[i] is the number of subarrays whose minimum is arr[i], summed modulo 10^9+7.

Reflection

  • Each index i is a minimum, bounded by the previous smaller on the left and the next smaller on the right. The count is (i - left) * (right - i).
  • Equals need one side strict and the other not. If both comparisons are <, the same minimum is counted twice.
  • Reduce mod before the product overflows. The sentinels are −1 and n. One element: the answer is itself.
Contributor review checklist

Interactive

Mental model

A worked animation for this problem. Scrub steps or press space to pause; re-tell the invariant out loud.

Step 1 of 6
1/6

Sum of subarray minimums for [3, 1, 2, 4]. Each value contributes val × left × right.

3
0
1
1
2
2
4
3

mod 10^9+7

Key:CurrentActive rangeMatchSettled