[LeetCode] 62. Unique Paths 不同路径

 

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The test cases are generated so that the answer will be less than or equal to 2 * 109

Example 1:

Input: m = 3, n = 7
Output: 28

Example 2:

Input: m = 3, n = 2
Output: 3
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down

Constraints:

  • 1 <= m, n <= 100

 

这道题让求所有不同的路径的个数,一开始还真把博主难住了,因为之前好像没有遇到过这类的问题,所以感觉好像有种无从下手的感觉。在网上找攻略之后才恍然大悟,原来这跟之前那道 Climbing Stairs 很类似,那道题是说可以每次能爬一格或两格,问到达顶部的所有不同爬法的个数。而这道题是每次可以向下走或者向右走,求到达最右下角的所有不同走法的个数。那么跟爬梯子问题一样,需要用动态规划 Dynamic Programming 来解,可以维护一个二维数组 dp,其中 dp[i][j] 表示到当前位置不同的走法的个数,由于每个位置只可能是由其上面或者右边过来,则当前的路径总数实际上就是从上面过来的路径总数加上右边过来的路径总数之和,然后可以得到状态转移方程为:  dp[i][j] = dp[i - 1][j] + dp[i][j - 1],参见代码如下:

 

解法一:

复制代码
class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> dp(m, vector<int>(n, 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];
    }
};
复制代码

 

这里为了节省空间,这里也可以使用一维数组 dp,一行一行的刷新也可以,代码如下:

 

解法二:

复制代码
class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<int> dp(n, 1);
        for (int i = 1; i < m; ++i) {
            for (int j = 1; j < n; ++j) {
                dp[j] += dp[j - 1]; 
            }
        }
        return dp[n - 1];
    }
};
复制代码

 

这道题其实还有另一种很数学的解法,参见网友 Code Ganker 的博客,实际相当于机器人总共走了 m + n - 2步,其中 m - 1 步向下走,n - 1 步向右走,那么总共不同的方法个数就相当于在步数里面 m - 1 和 n - 1 中较小的那个数的取法,实际上是一道组合数的问题,在初中时,我们就应该学过组合数的计算方法,就比如在n个数字中取k个数字的方法,就是 `n! / k!(n-k)!`,换到这里就是 `(m-n-2)! / (m-1)!(n-1)!`,写出代码如下:

 

解法三:

复制代码
class Solution {
public:
    int uniquePaths(int m, int n) {
        double num = 1, denom = 1;
        int small = m > n ? n : m;
        for (int i = 1; i <= small - 1; ++i) {
            num *= m + n - 1 - i;
            denom *= i;
        }
        return (int)(num / denom);
    }
};
复制代码

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/62

 

类似题目:

Unique Paths II

Minimum Path Sum

Dungeon Game

Minimum Path Cost in a Grid

Minimum Cost Homecoming of a Robot in a Grid

Number of Ways to Reach a Position After Exactly k Steps

Paths in Matrix Whose Sum Is Divisible by K

 

参考资料:

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

https://leetcode.com/problems/unique-paths/discuss/22981/My-AC-solution-using-formula

https://leetcode.com/problems/unique-paths/discuss/22954/0ms-5-lines-DP-Solution-in-C%2B%2B-with-Explanations

 

LeetCode All in One 题目讲解汇总(持续更新中...)

posted @   Grandyang  阅读(22963)  评论(7编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· MQ 如何保证数据一致性?
Fork me on GitHub

喜欢请打赏

扫描二维码打赏

Venmo 打赏

点击右上角即可分享
微信分享提示