Skip to content
Decoder

Sum of Subarray Minimums

Medium

Problem

Given a list of integers arr, look at every contiguous subarray and take its minimum. Return the sum of all those minimums. The answer can be huge, so return it 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], with minimums 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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.