DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Unique Path I & II

Unique Path I
Unique Path II

/**
 * @param {number} m
 * @param {number} n
 * @return {number}
 */
var uniquePaths = function(m, n) {
      let memo = new Map();

  const getAllPaths = (row, column) => {
    if (row === 0 && column === 0) {
      return 1;
    }
    if (row < 0 || column < 0) {
      return 0;
    }
    if (memo.has(row + "_" + column)) {
      return memo.get(row + "_" + column);
    }

    let up = getAllPaths(row - 1, column);
    let left = getAllPaths(row, column - 1);
    memo.set(row + "_" + column, up + left);
    return up + left;
  };
  return getAllPaths(m - 1, n - 1);
};

Enter fullscreen mode Exit fullscreen mode
/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
var uniquePathsWithObstacles = function(obstacleGrid) {
 let m = obstacleGrid.length;
  let n = obstacleGrid[0].length;
  let memo = new Map();
if (m === 1 && n === 1) {
    if (obstacleGrid[0][0] === 1) {
      return 0;
    }
    return 1;
  }

  const getAllPaths = (row, column) => {
    if (row === 0 && column === 0) {
     if (obstacleGrid[0][0] === 1) {
       return 0;
     }
     return 1;
    }
    if (row < 0 || column < 0 || obstacleGrid[row][column] === 1) {
      return 0;
    }
    if (memo.has(row + "_" + column)) {
      return memo.get(row + "_" + column);
    }

    let up = getAllPaths(row - 1, column);
    let left = getAllPaths(row, column - 1);
    memo.set(row + "_" + column, up + left);
    return up + left;
  };

  return getAllPaths(m - 1, n - 1);
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)