Unique Paths - LeetCode

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?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

思路:这道题有两种方法。

第一种是动态规划,构建一个m * n的数组dp, 然后dp[i][j] = dp[i - 1][j] + dp[i][j - 1]。

第二种是公式法,因为一共向下走的操作会有n - 1次,向右走的操作会有m - 1次,因此一共会走m + n - 2次。

然后从m + n - 2次操作中我们要选n - 1次向右走,因此这是一个组合问题。

排列公式 (n, m) = n! / (m!(n - m)!)

实际计算中,我们可以进一步整理为 (n - m + 1) * (n - m + 2) *...* (n - m + m) / m !

这里贴一下动态规划的代码

 1 class Solution {
 2 public:
 3     int uniquePaths(int m, int n) {
 4         vector<int> tem(n, 1);
 5         vector<vector<int> > dp(m, tem);
 6         for (int i = 1; i < m; i++)
 7             for (int j = 1; j < n; j++)
 8                 dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
 9         return dp[m - 1][n - 1];
10     }
11 };
时间: 2024-10-07 17:38:01

Unique Paths - LeetCode的相关文章

Unique Paths leetcode 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

题目链接: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 (ma

&lt;LeetCode OJ&gt; 63. Unique Paths II

63. Unique Paths II My Submissions Question Total Accepted: 55136 Total Submissions: 191949 Difficulty: Medium Follow up for "Unique Paths":紧接着上一题"唯一路劲",现在考虑有一些障碍在网格中,无法到达,请重新计算到达目的地的路线数目 Now consider if some obstacles are added to the

[LeetCode][JavaScript]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

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 &amp;&amp; Unique Paths II &amp;&amp; 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

[LeetCode][JavaScript]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 obsta

【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