[LeetCode]题解(python):063-Unique path II



题目来源



https://leetcode.com/problems/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.



题意分析



Input:a matrix as list[list[int]]

Output:the number of path

Conditions:从左上角走到左下角,注意有障碍不能通过



题目思路



上一题是用数学方法直接解,这一题用动态规划吧,因为每一步都会基于上一格或者是左一格,动态方程为dp[i][j] = dp[i][j-1] + dp[i-1][j]

注意初始化的时候如果遇到障碍,后面的位置不能初始化为0了



AC代码(Python)


 1 class Solution(object):
 2     def uniquePathsWithObstacles(self, obstacleGrid):
 3         """
 4         :type obstacleGrid: List[List[int]]
 5         :rtype: int
 6         """
 7         m = len(obstacleGrid)
 8         n = len(obstacleGrid[0])
 9         dp = [[0 for i in range(n)] for j in range(m)]
10         for i in range(n):
11             if obstacleGrid[0][i] == 1:
12                 break
13             else:
14                 dp[0][i] = 1
15
16         for i in range(m):
17             if obstacleGrid[i][0] == 1:
18                 break
19             else:
20                 dp[i][0] = 1
21
22         for i in range(1, m):
23             for j in range(1, n):
24                 if obstacleGrid[i][j] == 0:
25                     dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
26
27         return dp[m - 1][n - 1]
时间: 2024-10-28 10:45:49

[LeetCode]题解(python):063-Unique path II的相关文章

63. Unique Path II Leetcode Python

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 Path 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 middl

LeetCode 62/63/120 Unique PathsI/II Triangle--DP

一:unique Path 题目: 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 (ma

leetcode63—Unique Path 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 (marked 'Finish' in t

【LeetCode】063. 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 m

Java for LeetCode 063 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 middl

Unique path 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 middl

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

[LeetCode]题解(python):062 Unique path

题目来源 https://leetcode.com/problems/unique-paths/ 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 bott

[LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/unique-paths/ 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 rea