Created
January 31, 2017 04:27
-
-
Save railsstudent/d40bd874d773794ff358c3465a6d2f5d to your computer and use it in GitHub Desktop.
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
// http://stackoverflow.com/questions/8002252/euler-project-18-approach | |
function longestSlideDown (pyramid) { | |
let pyramidSum = []; | |
pyramid.forEach((r, i) => { | |
pyramidSum.push(r.map((e) => { | |
return (i === pyramid.length - 1) ? e : 0; | |
})); | |
}); | |
for (let i = pyramidSum.length - 2; i >= 0; i--) { | |
for (let j = 0; j < pyramidSum[i].length; j++) { | |
pyramidSum[i][j] = pyramid[i][j] + Math.max(pyramidSum[i+1][j], pyramidSum[i+1][j+1]); | |
} | |
} | |
return pyramidSum[0][0]; | |
} |
You always start from the top and have to find your way to the bottom. You can only slide to the
two adjacent fields downwards. Example: 1 -> [2, 3], 2 -> [4, 5], 3 -> [5, 6].
The number in each field represents how much you'll be slowed down by friction.
The fastest route is the route that has the lowest sum of all the fields passed through.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there anyway i could some modifications which will solve for smallest slide down? instead of taking largest ?