Skip to content

63. Unique Paths II

https://leetcode.com/problems/unique-paths-ii/

js
/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
var uniquePathsWithObstacles = function(obstacleGrid) {
  var m = obstacleGrid

  for (let i = 0; i < m.length; i++) {
    for (let j = 0; j < m[i].length; j++) {
      if (i === 0 && j === 0) {
        m[i][j] = 1 - m[i][j]
      } else if (i === 0) {
        m[i][j] = m[i][j] === 1 ? 0 : m[i][j - 1]
      } else if (j === 0) {
        m[i][j] = m[i][j] === 1 ? 0 : m[i - 1][j]
      } else {
        m[i][j] = m[i][j] === 1 ? 0 : m[i][j - 1] + m[i - 1][j]
      }
    }
  }

  var a = m[m.length - 1]
  return a[a.length - 1]
}
py
class Solution(object):

    def uniquePathsWithObstacles(self, obstacleGrid):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
        """
        m = obstacleGrid
        for i in range(len(m)):
            for j in range(len(m[i])):
                if i == 0 and j == 0:
                    m[i][j] = 1 - m[i][j]
                elif i == 0:
                    m[i][j] = 0 if m[i][j] == 1 else m[i][j - 1]
                elif j == 0:
                    m[i][j] = 0 if m[i][j] == 1 else m[i - 1][j]
                else:
                    m[i][j] = 0 if m[i][j] == 1 else m[i][j - 1] + m[i - 1][j]
        return m[-1][-1]