LeetCode 907 Sum of Subarray Minimums - Problem Statement

The problem

Given an array, find the sum of the minimum element of every contiguous subarray. Return the result modulo 109+710^9 + 7.

With [3, 1, 2, 4]:

  • Subarrays and their mins: [3]→3, [1]→1, [2]→2, [4]→4, [3,1]→1, [1,2]→1, [2,4]→2, [3,1,2]→1, [1,2,4]→1, [3,1,2,4]→1.
  • Sum = 3+1+2+4+1+1+2+1+1+1 = 17.

Instead of generating all subarrays, can you figure out how many times each element contributes as the minimum?

Constraints: 1n3×1041 \le n \le 3 \times 10^4.