There is such a pyramid of arrays:
[ [3], [7, 4], [2, 4, 6], [8, 5, 9, 3] ] It is necessary to find the maximum sum of its elements from top to bottom. Example:
/3/ \7\ 4 2 \4\ 6 8 5 \9\ 3 I do not even understand how to implement it yet. While it turns out to make the sum of columns:
function longestSlideDown(pyramid) { var result = []; for (var i = 0; i < pyramid.length; i++) { var subarr = pyramid[i]; for (var j = 0; j < subarr.length; j++) { if (result[j] == undefined) result[j] = 0; result[j] += subarr[j]; } } return result; } console.log(longestSlideDown([ [3], [7, 4], [2, 4, 6], [8, 5, 9, 3] ]));
3,7,4,9? - Yuri