Total Accepted: 86496 Total Submissions: 238592 Difficulty: Medium
time O(n*m) space o(n*m)
AC 0ms
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?
这道题也非常简单,没有带任何约束,并切限制了移动的方向,最后的点的路径dp[i][j] = dp[i][j-1] + dp[i-1][j]
代码如下
int uniquePaths(int m, int n) {
int **dp=malloc(sizeof(int*)*m);
for(int i=0;i<m;i++)
{
dp[i] = malloc(sizeof(int)*n);
memset(dp[i],0,sizeof(int)*n);
}
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
if(i==0||j==0)
{
dp[i][j]=1;
}
else
{
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
}
return dp[m-1][n-1];
}