Sum an Arbitrary-Dimensional Array
Problem statement
An arbitrary-dimensional integer array exposes its positive dimensions and stores its entries in row-major order in values, with the last dimension varying fastest.
Return the signed 64-bit sum of all entries. The product of the dimensions equals values.length.
Function
sumMultidimensionalArray(dimensions: int[], values: int[]) → longExamples
Example 1
dimensions = [2,2,2]values = [1,2,3,4,5,6,7,8]return = 36The eight entries of the 2 by 2 by 2 array sum to 36.
Example 2
dimensions = [3]values = [-5,2,9]return = 6A one-dimensional array follows the same contract.
Constraints
1 <= dimensions.length <= 121 <= dimensions[i]1 <= values.length <= 200000- The dimension product equals
values.length. - Each value fits in a signed 32-bit integer; the sum fits in a signed 64-bit integer.