LC 62 Unique Paths(M)
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?
边界判断 当m 和 n都为1时候,直接出结果。
解题思路 (组合计算, DP)
当成算数的组合计算,以 3x7为例,路径应该是 2个下,6个右的 组合 D R R D R R R R 这样的组合公式就是 (m + n)!/(m! * n!) 如果 m < n 公式简化为 (m + n) *...(m+1)/m!
当(m==0 || n==0)路径始终为1, 因为不能左或上, 其他情况
u(m, n) = u(m-1, n) + u(m, n-1);
Last updated
Was this helpful?