662. Sum of Total Strength of Wizards

HardStackMonotonic StackPrefix Sum

For an array strength, the strength of a contiguous group is the minimum value in the group multiplied by the sum of all values in the group. Return the sum of strengths over all non-empty contiguous groups, taken modulo 1000000007. The input is JSON {strength}.

Input: JSON {strength}.

Output: Integer — the total strength modulo 1e9+7.

Examples

Example 1
Input: {"strength":[1,3,1,2]}
Output: 44
Explanation: Sum over all groups is 44.
Example 2
Input: {"strength":[5,4,6]}
Output: 213
Explanation: Sum over all groups is 213.
Example 3
Input: {"strength":[7]}
Output: 49
Explanation: One group.

Constraints

Asked by

Amazon
Solve this problem in the editor →