Skip to content

Instantly share code, notes, and snippets.

@iJustErikk
Created December 7, 2020 17:17
Show Gist options
  • Save iJustErikk/fefd115cfefac1101f623aed7e9790f2 to your computer and use it in GitHub Desktop.
Save iJustErikk/fefd115cfefac1101f623aed7e9790f2 to your computer and use it in GitHub Desktop.
const gridTraveler = (m, n) => {
// create 2 dimensional array
const grid = new Array(m + 1)
.fill().map(() => new Array(n + 1).fill(0));
grid[1][1] = 1;
for (let i = 0; i < m + 1; i += 1) {
for (let j = 0; j < n + 1; j += 1) {
// add current value to right and down if possible
const currentValue = grid[i][j];
if (i < m) grid[i + 1][j] += currentValue;
if (j < n) grid[i][j+1] += currentValue;
}
}
return grid[m][n];
};
console.log(gridTraveler(20, 20));
console.log(gridTraveler(3,3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment