Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created February 16, 2020 22:01
Show Gist options
  • Save RP-3/65064cb7eed68961a45399d3bb46e5c7 to your computer and use it in GitHub Desktop.
Save RP-3/65064cb7eed68961a45399d3bb46e5c7 to your computer and use it in GitHub Desktop.
/**
* @param {number[][]} obstacleGrid
* @return {number}
*/
var uniquePathsWithObstacles = function(obstacleGrid) {
const memo = new Array(obstacleGrid.length)
.fill(0)
.map(() => new Array(obstacleGrid[0].length).fill(null));
const traverse = (i, j) => {
if(i === obstacleGrid.length) return 0;
if(j === obstacleGrid[0].length) return 0;
if(obstacleGrid[i][j] === 1) return 0;
if(i === obstacleGrid.length-1 && j === obstacleGrid[0].length-1){
return 1;
}
if(memo[i][j] !== null) return memo[i][j];
return memo[i][j] = traverse(i+1, j) + traverse(i, j+1);
};
return traverse(0, 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment