Grokking the Engineering Manager Coding Interview
0% completed
Sum of Subarray Minimums (medium)
Problem Statement
Given an array of integers arr, return the sum of the minimum values from all possible contiguous subarrays within arr. Since the result can be very large, return the final sum modulo (10<sup>9</sup> + 7).
Examples
Example 1:
- Input: arr =
[3, 1, 2, 4, 5] - Expected Output:
30 - Explanation:
- The subarrays are:
[3],[1],[2],[4],[5],[3,1],[1,2],[2,4],[4,5],[3,1,2],[1,2,4],[2,4,5],[3,1,2,4],[1, 2, 4, 5],[3, 1, 2, 4, 5]. - The minimum values of these subarrays are:
- The subarrays are:
.....
.....
.....
Like the course? Get enrolled and start learning!
S
singhursefamily
· a year ago
I believe the expected output in Example 3 should be 27, not 35. In fact, the sum in the last bullet point is 27.
Example 3:
- Input: arr =
[7, 3, 8] - Expected Output:
35 - Explanation:
- The subarrays are:
[7],[3],[8],[7,3],[3,8],[7,3,8]. - The minimum values of these subarrays are:
7, 3, 8, 3, 3, 3. - Summing these minimums:
7 + 3 + 8 + 3 + 3 + 3 = 27.
- The subarrays are: