LeetCode 62. Unique Paths
Description
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 7 x 3 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Example 1:
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 -> Right -> Down 2. Right -> Down -> Right 3. Down -> Right -> Right
Example 2:
Input: m = 7, n = 3 Output: 28
在m x n
的方格中,从左上角走到右下角的不同路径有几种?(只允许向右和向下走)
Solution
这道题目可以转换为数学题:
m x n
的方格要从左上角走到右下角,需要经过m - 1
次Right
和n - 1
次Down
操作。
所以总共是m + n - 2
次操作,而操作顺序的不同,则对应路径不同。
这就转换成了数学的排列问题:C(m + n -2, n - 1)
。
在m + n - 2
操作中取Down
操作的位置,其余位置就是Right
操作。
|
|