LeetCode --- 62. Unique Paths

题目链接: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 bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).

How many possible unique paths are there?

Note: m and n will be at most 100.

这道题的要求是计算机器人从左上角移动到右下角有多少不同的路径(机器人每次只能向右或是向下移动1格)。

1. 动态规划

这是一道简单的动态规划问题,假设机器人当前在[i, j]位置,由于其只能向下或向右移动,因此只能由[i, j-1]和[i-1, j]这两位置移动到[i, j]。因此到达[i, j]位置的不同路径数量为[i, j-1]和[i-1, j]两位置的路径数量之和,即dp[i][j] = dp[i][j-1] + dp[i-1][j]。

时间复杂度:O(mn)

空间复杂度:O(mn)

 1 class Solution{
 2 public:
 3     int uniquePaths(int m, int n)
 4     {
 5         vector<vector<int> > vvi(m + 1, vector<int>(n + 1, 0));
 6         vvi[0][1] = 1;
 7         for(int i = 1; i <= m; ++ i)
 8             for(int j = 1; j <= n; ++ j)
 9                 vvi[i][j] = vvi[i][j - 1] + vvi[i - 1][j];
10         return vvi[m][n];
11     }
12 };

2. 组合问题

这同样是一道组合问题。由于机器人只能向下或向右移动,因此从左上角移动到右下角,需要向下移动m-1步,向右移动n-1步,即在m+n-2步中选出m-1步向下移动或是选出n-1步向右移动,即C(m+n-2, m-1)或C(m+n-2, n-1)。令minmn为m和n中的较小值,则共有C(m+n-2, minmn-1)不同路径。

至于求组合数,可以利用杨辉三角,逐层加出来即可。

时间复杂度:O((m+n)*min(m,n))

空间复杂度:O((m+n)*min(m,n))

 1 class Solution{
 2 public:
 3     int uniquePaths(int m, int n)
 4     {
 5         vector<vector<int> > vvi(m + 1, vector<int>(n + 1, 0));
 6         vvi[0][1] = 1;
 7         for(int i = 1; i <= m; ++ i)
 8             for(int j = 1; j <= n; ++ j)
 9                 vvi[i][j] = vvi[i][j - 1] + vvi[i - 1][j];
10         return vvi[m][n];
11     }
12 };

转载请说明出处:LeetCode --- 62. Unique Paths

时间: 2024-10-13 12:25:55

LeetCode --- 62. Unique Paths的相关文章

leetCode 62.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 bottom-right corner of the grid (marked 'Finish' in t

LeetCode 62. Unique Paths Java

题目: 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'

[LeetCode] 62.Unique Paths I

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 (marke

19.2.9 [LeetCode 62] 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 bottom-right corner of the grid (marked 'Finish' in t

[Leetcode]@python 62. Unique Paths

题目链接:https://leetcode.com/problems/unique-paths/ 题目大意:给定n.m,在mxn的矩阵中,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走) 解题思路:从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右.也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1) class Solution(object): def uniquePaths(self, m, n): ""&

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】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 bottom-right corner of the grid (marked 'Finish' in t

【Leetcode】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

62. Unique Paths i &amp; ii

62. 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 bottom-right corner of the grid (mar