资讯专栏INFORMATION COLUMN

[Leetcode] Unique Paths 唯一路径

yangrd / 3111人阅读

摘要:动态规划复杂度时间空间思路因为要走最短路径,每一步只能向右方或者下方走。所以经过每一个格子路径数只可能源自左方或上方,这就得到了动态规划的递推式,我们用一个二维数组储存每个格子的路径数,则。

Unique Paths I

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 the diagram below).

How many possible unique paths are there?

动态规划 复杂度

时间 O(NM) 空间 O(NM)

思路

因为要走最短路径,每一步只能向右方或者下方走。所以经过每一个格子路径数只可能源自左方或上方,这就得到了动态规划的递推式,我们用一个二维数组dp储存每个格子的路径数,则dp[i][j]=dp[i-1][j]+dp[i][j-1]。最左边和最上边的路径数都固定为1,代表一直沿着最边缘走的路径。

代码
public class Solution {
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m][n];
        for(int i = 0; i < m; i++){
            dp[i][0] = 1;
        }
        for(int i = 0; i < n; i++){
            dp[0][i] = 1;
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }
}
一维数组 复杂度

时间 O(NM) 空间 O(N)

思路

实际上我们可以复用每一行的数组来节省空间,每个元素更新前的值都是其在二维数组中对应列的上一行的值。这里dp[i] = dp[i - 1] + dp[i];

代码
public class Solution {
    public int uniquePaths(int m, int n) {
        int[] dp = new int[n];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                // 每一行的第一个数都是1
                dp[j] = j == 0 ? 1 : dp[j - 1] + dp[j];
            }
        }
        return dp[n - 1];
    }
}
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.

Note: m and n will be at most 100.

动态规划 复杂度

时间 O(NM) 空间 O(NM)

思路

解法和上题一模一样,只是要判断下当前格子是不是障碍,如果是障碍则经过的路径数为0。需要注意的是,最上面一行和最左边一列,一旦遇到障碍就不再赋1了,因为沿着边走的那条路径被封死了。

代码
public class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        for(int i = 0; i < m; i++){
            if(obstacleGrid[i][0] == 1) break;
            dp[i][0] = 1;
        }
        for(int i = 0; i < n; i++){
            if(obstacleGrid[0][i] == 1) break;
            dp[0][i] = 1;
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = obstacleGrid[i][j] != 1 ? dp[i-1][j] + dp[i][j-1] : 0;
            }
        }
        return dp[m-1][n-1];
    }
}
一维数组 复杂度

时间 O(NM) 空间 O(N)

思路

和I中的空间优化方法一样,不过这里判断是否是障碍物,而且每行第一个点的值取决于上一行第一个点的值。

代码
public class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int[] dp = new int[obstacleGrid[0].length];
        for(int i = 0; i < obstacleGrid.length; i++){
            for(int j = 0; j < obstacleGrid[0].length; j++){
                dp[j] = obstacleGrid[i][j] == 1 ? 0 : (j == 0 ? (i == 0 ? 1 : dp[j]) : dp[j - 1] + dp[j]); 
            }
        }
        return dp[obstacleGrid[0].length - 1];
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64447.html

相关文章

  • leetcode63. Unique Paths II

    摘要:题目要求这是题目系列。存在路障的节点会在数组中被标记为。请问从起点到终点有多少条独立路径。思路和代码在的思路基础上,我们可以知道,如果某个节点上存在路障,那么任何从该节点前往终点的路径都将不存在。也就是说,该节点的路径数为。 题目要求 Follow up for Unique Paths: Now consider if some obstacles are added to the...

    eternalshallow 评论0 收藏0
  • leetcode62. Unique Paths

    摘要:问一共有多少条独特的路线思路一通过递归实现计算。思路二杨辉三角在思路的指引下,我们可以尝试将递归的方法改变为循环的方法来解决。我们只需要确定在总部数中哪些步数右行或是哪些步数下行即可知道其对应的路径。这里运用到排列组合的思想。 题目要求 A robot is located at the top-left corner of a m x n grid (marked Start in ...

    zqhxuyuan 评论0 收藏0
  • [LintCode/LeetCode] Unique Paths II

    摘要:和完全一样的做法,只要在初始化首行和首列遇到时置零且即可。对了,数组其它元素遇到也要置零喏,不过就不要啦。 Problem Follow up for Unique Paths: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle...

    firim 评论0 收藏0
  • [LintCode/LeetCode] Unique Paths

    摘要:简单的动规题目,建立数组。坐标为矩阵的坐标,值为从左上角到这一格的走法总数。赋初值,最上一行和最左列的所有格子的走法都只有一种,其余格子的走法等于其左边格子走法与上方格子走法之和。最后,返回即可。 Problem A robot is located at the top-left corner of a m x n grid (marked Start in the diagram ...

    Gu_Yan 评论0 收藏0
  • 力扣(LeetCode)609

    摘要:一组重复的文件至少包括二个具有完全相同内容的文件。如果,则表示该目录是根目录。该输出是重复文件路径组的列表。文件路径是具有下列格式的字符串示例输入输出注最终输出不需要顺序。给定的文件数量在,个范围内。 题目地址:https://leetcode-cn.com/probl...题目描述:给定一个目录信息列表,包括目录路径,以及该目录中的所有包含内容的文件,您需要找到文件系统中的所有重复文...

    xietao3 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<