Sum of Subarray Minimums

Medium Topics Company Tags

For every nonempty contiguous subarray of arr, take its minimum value. Return the sum of those minimums, modulo 1000000007.

Example 1:

Input: arr = [2,1,2]

Output: 8

Explanation: The three length-1 subarrays contribute 2 + 1 + 2 = 5. The two length-2 subarrays each contribute 1, and the whole array contributes 1, for a total of 8.

Example 2:

Input: arr = [5]

Output: 5

Explanation: The only nonempty subarray is [5], whose minimum is 5.

Example 3:

Input: arr = [4,4,4]

Output: 24

Explanation: There are 3 + 2 + 1 = 6 nonempty subarrays. Each has minimum 4, so the sum is 6 * 4 = 24.

Constraints:

  • 1 <= arr.length <= 30000
  • 1 <= arr[i] <= 30000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.