Amazon | OA | Contiguous subarrays sum

Given an array of integers, for each contiguous subarray calculate the following value:
power = subarray sum * minimum element of subarray

Return the sum of power values for every contiguous subarray in the array.

Example input: {2,3,1,4}
Expected output = 73

Explanation:
2 -> 2 * 2 = 4
2,3 -> (2 + 3) * 2 = 10
2,3,1 -> (2 + 3 + 1) * 1 = 6
2,3,1,4 -> (2 + 3 + 1 + 4) * 1 = 10
3 -> 3 * 3 = 9
3,1 -> (3 + 1) * 1 = 4
3,1,4 -> (3 + 1 + 4) * 1 = 8
1 -> 1 * 1 = 1
1, 4 -> (1 + 4) * 1 = 5
4 -> 4 * 4 = 16

Total = 73

I could only think of the brute force solution, still can't think of a way to optimise it. Anyone got a good solution or know a similar leetcode problem?

Comments (2)