Sum of Subarray Minimums

Given an array of integers arr, find the sum of min(b), where b ranges over every contiguous subarray of arr. Since the answer may be large, return the answer modulo 10^9 + 7.

Example 1
Inputarr = [3,1,2,4]
Output17
The subarray minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, and 1, whose sum is 17.
Example 2
Inputarr = [11,81,94,43,3]
Output444
The sum of the minimum values over all contiguous subarrays is 444.

Constraints

  • 1 <= arr.length <= 3 * 10^4
  • 1 <= arr[i] <= 3 * 10^4

Asked at 13 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate