Created
February 16, 2020 22:01
-
-
Save RP-3/65064cb7eed68961a45399d3bb46e5c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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