Sum of Subarray Minimums
MediumStack
Decode it first. Clues, constraints, pattern and plan before any code.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.
arr = [3, 1, 2, 4] → 17The 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.
arr = [11, 81, 94, 43, 3] → 444Constraints
1 <= arr.length <= 3 * 10^41 <= arr[i] <= 3 * 10^4