leetcode 62. 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?

picture_1
Note: m and n will be at most 100.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int uniquePaths(int m, int n) {
int i = 0,j = 0;
if(m <= 0 || n <= 0)
{
return 0;
}
int dp[m][n];
for(i = 0;i < m;i++)
{
dp[i][0] = 1;
}
for(j = 0;j < n;j++)
{
dp[0][j] = 1;
}
for(i = 1;i < m;i++)
{
for(j = 1;j < n;j++)
{
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}