[leetcode DP]63. Unique Paths II

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]

The total number of unique paths is 2.

障碍物对应的dp表中设为0即可,为了使代码简洁,多加了两行dp[i][0]和dp[0][j]

 1 class Solution(object):
 2     def uniquePathsWithObstacles(self, obstacleGrid):
 3         m,n = len(obstacleGrid),len(obstacleGrid[0])
 4         dp = [[0 for j in range(n+1)] for i in range(m+1)]
 5         dp[0][1] = 1
 6         for i in range(1,m+1):
 7             for j in range(1,n+1):
 8                 if not obstacleGrid[i-1][j-1]:
 9                     dp[i][j] = dp[i][j-1] + dp[i-1][j]
10         return dp[m][n]
11         

还有一种占用O(N)空间的方法,感觉挺不错的

思路:用一个数组tmp记录每一行中每一个位置拥有的次数,每到一个没有障碍物的位置,那么这个位置自动继承上一个位置上所拥有的次数

 1 class Solution(object):
 2     def uniquePathsWithObstacles(self, obstacleGrid):
 3         m,n=len(obstacleGrid),len(obstacleGrid[0])
 4         tmp = [0]*(n+1)
 5         tmp [n-1] = 1
 6         for i in range(m-1,-1,-1):
 7             for j in range(n-1,-1,-1):
 8                 if obstacleGrid[i][j]:
 9                     tmp[j] = 0
10                 else:
11                     tmp[j] += tmp [j+1]
12         return tmp[0]
13         
时间: 2024-08-09 10:38:05

[leetcode DP]63. Unique Paths II的相关文章

<LeetCode OJ> 63. Unique Paths II

63. Unique Paths II My Submissions Question Total Accepted: 55136 Total Submissions: 191949 Difficulty: Medium Follow up for "Unique Paths":紧接着上一题"唯一路劲",现在考虑有一些障碍在网格中,无法到达,请重新计算到达目的地的路线数目 Now consider if some obstacles are added to the

【一天一道LeetCode】#63. Unique Paths II

一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one ob

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one

LeetCode 62, 63 Unique Paths I+II

62. Unique Paths 空间可以按行优化为 O(n),也可以按列优化为O(m). class Solution { public: int uniquePaths(int m, int n) { int dp[m][n]={0}; dp[0][0]=1; for (int i=0;i<m;++i){ for (int j=0;j<n;++j){ if (i==0&&j==0) continue; dp[i][j]=0; if (j-1>=0) dp[i][j]

LeetCode开心刷题二十九天——63. Unique Paths II**

63. Unique Paths II Medium 938145FavoriteShare A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom

63. Unique Paths II(js)

63. Unique Paths II A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (

leetCode 63.Unique Paths II (唯一路径II) 解题思路和方法

Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obsta

63. Unique Paths II (Graph; DP)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

LeetCode 63. Unique Paths II Java

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the m