56. OA - Security Risk of Data Center Segments

OA - Security Risk of Data Center Segments
An application at Amazon is deployed on 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.

Method Signature

public int calculateTotalRisk(List<Integer> security)
  • 1 ≤ security.size() ≤ 105

Examples

Example 1

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

Example 2

k = 4
security = [4, 2, 1, 2]

Output: 59

Example 3

k = 3
security = [1, 1, 1]

Output: 10





Please use Laptop/Desktop or any other large screen to add/edit code.