1008. Total Squad Strength

A coach lines up soldiers, where strength[i] is the strength of the soldier at position i. Any contiguous block of soldiers can be sent on a mission. The mission power of a block is its weakest member's strength multiplied by the sum of all strengths in the block.

Add up the mission power of every non-empty contiguous block (blocks at different positions count separately, even if they contain the same numbers) and return the total modulo 1000000007.

The total is far too large for a plain 32-bit integer, so keep every intermediate value reduced. Aim for O(n) time and O(n) space.

Example 1

Input:
strength = [7,3,9,4]
Output:
447
Explanation:

Summing weakest-member times total over every contiguous formation gives 447.

Example 2

Input:
strength = [12,12,5]
Output:
831
Explanation:

Summing weakest-member times total over every contiguous formation gives 831.

Constraints

1 ≤ strength.length ≤ 105

1 ≤ strength[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…