Skip to content

Instantly share code, notes, and snippets.

@iJustErikk
Created December 4, 2020 19:36
Show Gist options
  • Save iJustErikk/a0d6bf30c272d16c06dae4452db9218a to your computer and use it in GitHub Desktop.
Save iJustErikk/a0d6bf30c272d16c06dae4452db9218a to your computer and use it in GitHub Desktop.
const gridTraveler = (x, y, z, memo = new Map()) => {
const key = `${x},${y},${z}`;
if (memo.has(key)) return memo.get(key);
if (x === 0 || y === 0 || z === 0) return 0;
if (x === 1 && y === 1 && z === 1) return 1;
memo.set(key, gridTraveler(x - 1, y, z, memo) + gridTraveler(x, y - 1, z, memo)) + gridTraveler(x, y, z - 1, memo));
return memo.get(key);
};
console.log(gridTraveler(10, 10, 10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment