Skip to content

Instantly share code, notes, and snippets.

@Muzietto
Last active January 5, 2017 17:27
Show Gist options
  • Save Muzietto/a8f9c1d5807eb1d0555a33a896024fe2 to your computer and use it in GitHub Desktop.
Save Muzietto/a8f9c1d5807eb1d0555a33a896024fe2 to your computer and use it in GitHub Desktop.
Dynamic programming - Playing all possible solitaires on a board, throwing one die, up to the last square.
function numberSolitaire(arra) {
var dp = new Array(arra.length).fill(0);
dp[0] = arra[0];
for (var i = 1; i < arra.length; i++) {
var max = -Infinity;
for (var j = 1; j <= 6; j++) {
if (i - j < 0) break;
max = Math.max(max, dp[i-j] + arra[i]);
}
dp[i] = max;
}
return dp[arra.length-1];
}
@Muzietto
Copy link
Author

Muzietto commented Jan 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment