LeetCode: Unique Paths II Solution
1/**2 * @param {number[][]} obstacleGrid3 * @return {number}4 */5var uniquePathsWithObstacles = function (obstacleGrid) {6 const [M, N] = [obstacleGrid.length, obstacleGrid[0].length]7 const memo = Array.from({ length: M }, _ => Array(N).fill(null))89 const recursion = (i, j) => {10 if (memo[i][j] !== null) {11 return memo[i][j]12 }1314 if (obstacleGrid[i][j] === 1) {15 return 016 }1718 if (i === M - 1 && j === N - 1) {19 return 120 }2122 let numberOfWays = 023 if (i < M - 1) {24 numberOfWays += recursion(i + 1, j)25 }26 if (j < N - 1) {27 numberOfWays += recursion(i, j + 1)28 }2930 return (memo[i][j] = numberOfWays)31 }3233 return recursion(0, 0)34}
Comments
Loading comments...
Tags
leetcode
array
dynamic programming
recursion
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.