Skip to content

Instantly share code, notes, and snippets.

@KimSarabia
Last active October 10, 2017 02:47
Show Gist options
  • Save KimSarabia/a952d1e518ffe76fd5f5ce5914d3746c to your computer and use it in GitHub Desktop.
Save KimSarabia/a952d1e518ffe76fd5f5ce5914d3746c to your computer and use it in GitHub Desktop.
pyramid-slide-down.js
function longestSlideDown (pyramid) {
var pyramidClone = pyramid.slice();
for(var i = pyramid.length - 2; i >= 0; i--){
current = pyramid[i]
if(i === pyramid.length - 2){
next = pyramidClone[i+1]
} else {
next = newArr
}
newRow = []
for(var j = 0; j < current.length; j++){
leftChild = next[j]
rightChild = next[j+1]
bigger = Math.max(leftChild,rightChild)
newRow.push(current[j] + bigger)
}
newArr = newRow
}
return newArr[0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment