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] ])); 

  • Do you just need to display the maximum value from all the arrays? - Yuri
  • I need to find the maximum sum of elements from all arrays from top to bottom, as I showed in the example above ... - lesha310392
  • Those. withdraw 3,7,4,9 ? - Yuri
  • withdraw their amount ie the answer should be 23 - lesha310392
  • And where does the top down? - Yuri

3 answers 3

It is necessary to add the maximum result to the result.

 Массив 1: Макс. = 3 Массив 2: Макс. = 7 Массив 3: Макс. = 6 Массив 4: Макс. = 9 

Here is a working example:

 function longestSlideDown(pyramid) { var result = 0; for (var i = 0; i < pyramid.length; i++) { var subarr = pyramid[i]; var max = 0; for (var j = 0; j < subarr.length; j++) { if(max < subarr[j]){ max = subarr[j]; }; }; result = result + max; }; return result; }; console.log(longestSlideDown([ [3], [7, 4], [2, 4, 6], [8, 5, 9, 3] ])); 

Here is a working example on the condition of the problem of the author of the question:

 function longestSlideDown(pyramid) { var result = 0, maxElem = null; // Π—Π°Π΄Π°Ρ‘ΠΌ максимальноС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π² массимС for (var i = 0; i < pyramid.length; i++) { var subarr = pyramid[i]; var max = 0; // МаксимальноС число if(maxElem == null){ // Если индСкса максимального значСния Π½Π΅Ρ‚, Ρ‚ΠΎ считаСм ΠΏΠΎ ΠΎΠ±Ρ‹Ρ‡Π½ΠΎΠΌΡƒ ΠΏΡ€ΠΈΠ½Ρ†ΠΈΠΏΠ΅ var maxVal = pyramid[i].length, minVal = 0 }else{ // Если Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ максимального Π΅ΡΡ‚ΡŒ, Ρ‚ΠΎ ΠΈΡ‰Π΅ΠΌ элСмСнты с ΠΎΡ‚ΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ΠΌ Π² ΠΎΠ΄ΠΈΠ½ индСкс ΠΎΡ‚ ΠΏΡ€ΠΎΡˆΠ»ΠΎΠ³ΠΎ максимального var maxVal = maxElem + 1, minVal = maxElem - 1 }; for (var j = minVal; j <= maxVal; j++) { if(max < subarr[j]){ maxElem = j; max = subarr[j]; }; }; result = result + max; }; return result; }; console.log(longestSlideDown([ [3], [7, 4], [2, 4, 6], [8, 5, 9, 3] ])); 

  • @ lesha310392, from which he should deduce 23? If you add up all the maximum values ​​will be 25 - Yuri
  • According to the conditions of the problem, the answer is 23 ... I gave an example at the top of how the elements should be considered ... so I can’t catch up with how to do so - lesha310392
  • @ lesha310392, I caught up. Now I will try to do it - Yuri
  • This is some kind of absurdity! Why on the second from the top is a shift to the left, and on the second from the right is a shift to the right? - user220409
  • @ lesha310392, see an example on your question - Yuri

I use feature reduce and transformation of an array with one element. If an array with one element is converted, it becomes a string. As a result, it turns out that the first iteration in last transmits a string, which I convert to a number and add it to the next. value of the next by array list.

 const pyramid = [ [3], [7, 4], [2, 4, 6], [8, 5, 9, 3] ]; const a = pyramid.reduce((last, cur, i) => { return parseInt(last) + cur[i - 1] }); console.log(a); 

     let arr = [ [3], [7, 4], [2, 4, 6], [8, 5, 9, 3] ]; console.info(arr.reduce((a, _) => a += Math.max(..._), 0));