Last active
October 10, 2017 02:47
-
-
Save KimSarabia/a952d1e518ffe76fd5f5ce5914d3746c to your computer and use it in GitHub Desktop.
pyramid-slide-down.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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