Using Dynamic Programming (Bottom up approach)


#1

int Solution::uniquePaths(int A, int B) {
vector<vector> ways(A,vector(B,0));
for(int i=A-1;i>=0;i–)
{
for(int j=B-1;j>=0;j–)
{
if(j==B-1 && i==A-1)
{
ways[i][j]=1;

         }
         else
         {
             if(i<A-1 && i>=0)
                ways[i][j]+=ways[i+1][j];
                
                if(j<B-1 && j>=0)
                 ways[i][j]+=ways[i][j+1];
         }
         
         
     }
}
return ways[0][0];

}