Unique Paths
第25天。
感冒真难受!
今天的题目是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.
如果自己尝试几次去找的话,我们可以发现在m=1时或n=1时,只要一种可能,然后我们考虑我们现在位于(m,n)点(m!=1,n!=1),现在要走向(0,0),因为我们被限制到只能向下或下右走,即只能n-1或m-1,所以我们可以找到这样一个等式uniquePaths(m,n) = uniquePaths(m-1,n) + uniquePaths(m,n-1),当m!=1,n!=1时,所以我们可以很快的写出下面的解决方案:
1 | int uniquePaths(int m, int n) { |
但是这个会时间超限,因为我们做了很多重复的计算,因为我们计算uniquePaths(3,2)时需要计算uniquePaths(2,2),而uniquePaths(2,1)也需要计算一遍,这就导致了很多重复计算。
我们考虑使用一个表来记录uniquePaths值,这样就可以减少每次计算的值了:
1 | int uniquePaths(int m, int n) { |
这样就不会时间超限了。
然后是在dicuss中看到的:
1 | int uniquePaths(int m, int n) { |
(捂脸)我一直不知道怎么用vector来快速的构造二维数组。
上面的两个的空间复杂度都是O(m*n),下面的方法可以降成O(2*min(m,n)),因为我们计算某一层的时候其实只需要前一行即可:
1 | int uniquePaths(int m, int n) { |
然后其实还可以继续降低到O(min(m,n)),只需要一个一维数组即可:
1 | int uniquePaths(int m, int n) { |