Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created January 31, 2017 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save railsstudent/d40bd874d773794ff358c3465a6d2f5d to your computer and use it in GitHub Desktop.
Save railsstudent/d40bd874d773794ff358c3465a6d2f5d to your computer and use it in GitHub Desktop.
// 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];
}
@ashishrao6666
Copy link

Is there anyway i could some modifications which will solve for smallest slide down? instead of taking largest ?

@ashishrao6666
Copy link

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