k data centers connected linearly. The security level of the ith data center is given by security[i]. The security risk of a contiguous segment of data centers, i.e. a subsegment, is defined as the product of the number of data centers and the maximum security level of the data centers in the segment.
Given the array security, find the sum of the security risk of all the segments of the data centers. Since the answer can be large, report it modulo 109 + 7.
public int calculateTotalRisk(List<Integer> security)
k = 3
security = [3, 2, 3]
| Subsegment | Number of data centers | Maximum Security Level | Security Risk |
|---|---|---|---|
| [3] | 1 | 3 | 1 * 3 = 3 |
| [3, 2] | 2 | 3 | 2 * 3 = 6 |
| [3, 2, 3] | 3 | 3 | 3 * 3 = 9 |
| [2] | 1 | 2 | 1 * 2 = 2 |
| [2, 3] | 2 | 3 | 2 * 3 = 6 |
| [3] | 1 | 3 | 1 * 3 = 3 |
The sum of all security risks is 3 + 6 + 9 + 2 + 6 + 3 = 29.
Output: 29
k = 4
security = [4, 2, 1, 2]
Output: 59
k = 3
security = [1, 1, 1]
Output: 10