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?
幸亏long long 可以搞定,不过数量更多之后,还是需要其他办法。
class Solution {
public:
int uniquePaths(int m, int n) {
if(m == 1 || n == 1) return 1;
int down = m + n - 2;
int up = m < n ? m - 1 : n -1;
long long totaldown = 1;
long long totalup = 1;
for(int ii = up; ii > 0; ii --) {
totaldown *= down;
totalup *= up;
down --;
up --;
}
return totaldown / totalup;
}
};