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 is there?
Above is a 3 x 7 grid. How many possible unique paths is there?
Note: m and N would be is at most 100.
Can be pushed to the formula, for m+1,n+1, only need to go to the right M step, go down n steps, you can reach the end, we put down and to the right, all the permutation is the result result= (m+n)!/(m!*n!) The direct request will go out of scope, so simplify a little bit: m+1 (m/2+1) ... (m/n+1)
1 classSolution {2 Public:3 4 intUniquepaths (intMintN) {5 6 intresult=0;7 Doubletmp=1;8m--;9n--;Ten for(intI=0; i<n;i++) One { ATmp*= (Double) (m)/(i+1)+1; - } - theresult=round (TMP); - returnresult; - } -};
Direct use of dynamic programming
1 classSolution {2 Public:3 4 intUniquepaths (intMintN) {5 6 if(m==0|| n==0)return 0;7 8 intdp[101][101];9 Tendp[0][0]=1; One A for(intI=1; i<m;i++) - { -dp[i][0]=1; the } - - for(intj=1; j<n;j++) - { +dp[0][j]=1; - } + A for(intI=1; i<m;i++) at { - for(intj=1; j<n;j++) - { -dp[i][j]=dp[i][j-1]+dp[i-1][j]; - } - } in returndp[m-1][n-1]; - } to};
"Leetcode" Unique Paths