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:DP O(N^2) with O(N^2) 空间
很直接的2维dp问题,对于任意一个点i,j,到达它的办法可以从上面过来,也可以从左面过来。
所以总的办法数是dp[i][j] = dp[i - ][j] + dp[i][j -1]
结果就是dp[m - 1][n - 1]
Java
解法2:DP O(N^2) with O(N) 空间
和triangle相类似的思路,dp的过程是自上而下自左而右,那么可以用滚动数组来减少内存的使用。