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.
Example
1,1 | 1,2 | 1,3 | 1,4 | 1,5 | 1,6 | 1,7 |
2,1 | ||||||
3,1 | 3,7 |
Above is a 3 x 7 grid. How many possible unique paths are there
class Solution {
public:
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
int uniquePaths(int m, int n) {
vector<vector<int>> UP(m, vector<int>(n, 1));
for(int i = m-2; i >= 0; i--)
for(int j = n-2; j >= 0; j--)
UP[i][j] = UP[i+1][j] + UP[i][j + 1];
return UP[0][0];
}
};
class Solution {
public:
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
int uniquePaths(int m, int n) {
vector<int> UP(n, 1);
for(int i = m-2; i >= 0; i--)
for(int j = n-2; j >= 0; j--)
UP[j] = UP[j] + UP[j + 1];
return UP[0];
}
};
No comments:
Post a Comment