← 返回 amazon 的题目列表Find the Sum of Maximum Subarray Products
类型:online_judge
Given an integer array, calculate the sum of each subarray's maximum value multiplied by its length, maintaining an overall time complexity of O(n).
Input
An integer array arr.
Output
The total sum of each subarray's maximum value multiplied by its length.
Example
Input: arr = [4,2,1,2]
Output: 59
Example Explanation
The subarray [4] has a maximum value 4 with length 1, giving a product of 4.
The subarray [4, 2] has a maximum value 4 with length 2, giving a product of 8.
The subarray [4, 2, 1] has a maximum value 4 with length 3, giving a product of 12.
The subarray [4, 2, 1, 2] has a maximum value 4 with length 4, giving a product of 16.
The subarray [2] has a maximum value 2 with length 1, giving a product of 2.
The subarray [2, 1] has a maximum value 2 with length 2, giving a product of 4.
The subarray [2, 1, 2] has a maximum value 2 with length 3, giving a product of 6.
The subarray [1] has a maximum value 1 with length 1, giving a product of 1.
The subarray [1, 2] has a maximum value 2 with length 2, giving a product of 4.
The subarray [2] has a maximum value 2 with length 1, giving a product of 2.
Sum of all products is 59.
Example
Input
4 2 1 2