Sum of Subarray Minimums
Medium · Array, Dynamic Programming, Stack, Monotonic Stack
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 109 + 7.
Example 1
Input: arr = [3,1,2,4] Output: 17
Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1. Sum is 17.
Example 2
Input: arr = [11,81,94,43,3] Output: 444
Constraints
- 1 <= arr.length <= 3 * 104
- 1 <= arr[i] <= 3 * 104
Starter code (python)
class Solution(object):
def sumSubarrayMins(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
Related problems
- Two Sum (Easy)
- Median of Two Sorted Arrays (Hard)
- Longest Palindromic Substring (Medium)
Previous: Sort Array By Parity · Next: Smallest Range I · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.