Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 62. Unique Paths #62

Open
grandyang opened this issue May 30, 2019 · 1 comment
Open

[LeetCode] 62. Unique Paths #62

grandyang opened this issue May 30, 2019 · 1 comment

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019


请点击下方图片观看讲解视频
Click below image to watch YouTube Video
Video

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 同步地址:

#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 题目讲解汇总(持续更新中...)

(欢迎加入博主的知识星球,博主将及时答疑解惑,并分享刷题经验与总结,快快加入吧~)

知识星球 喜欢请点赞,疼爱请打赏❤️~.~

微信打赏

|

Venmo 打赏


---|---

@lld2006
Copy link

lld2006 commented Jul 7, 2020

解法1可以再check一下m n的大小, 来改善space complexity。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants