<p>There is a robot on an <code>m x n</code> grid. The robot is initially located at the <strong>top-left corner</strong> (i.e., <code>grid[0][0]</code>). The robot tries to move to the <strong>bottom-right corner</strong> (i.e., <code>grid[m - 1][n - 1]</code>). The robot can only move either down or right at any point in time.</p>

<p>Given the two integers <code>m</code> and <code>n</code>, return <em>the number of possible unique paths that the robot can take to reach the bottom-right corner</em>.</p>

<p>The test cases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" style="width: 400px; height: 183px;" />
<pre>
<strong>Input:</strong> m = 3, n = 7
<strong>Output:</strong> 28
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> m = 3, n = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -&gt; Down -&gt; Down
2. Down -&gt; Down -&gt; Right
3. Down -&gt; Right -&gt; Down
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= m, n &lt;= 100</code></li>
</ul>

class Solution {
public:
    
    //1st solution recusion gives TLE

    // int solve(int i, int j, int m, int n)
    // {
    //     if(i==m-1 && j==n-1)
    //     {
    //         return 1;
    //     }
    //     if(i>=m || j>=n)
    //     {
    //         return 0;
    //     }
    //     return solve(i+1,j,m,n)+solve(i,j+1,m,n);
    // }

    //2nd solution Now using DP

    // int solve(int i, int j, int m, int n, vector<vector<int>>&dp)
    // {
    //     if(i==m-1 && j==n-1)
    //     {
    //         return 1;
    //     }
    //     if(i>=m || j>=n)
    //     {
    //         return 0;
    //     }
    //     if(dp[i][j]!=-1)
    //     {
    //         return dp[i][j];
    //     }
    //     return dp[i][j]=solve(i+1,j,m,n,dp)+solve(i,j+1,m,n,dp);
    // }

    int uniquePaths(int m, int n) {

        //1st solution recusion gives TLE

        // int ans=solve(0,0,m,n);
        // return ans;

        //2nd solution Now using DP

        // vector<vector<int>>dp(m+1,vector<int>(n+1,-1));
        // int num=solve(0,0,m,n,dp);
        // if(m==1 && n==1)
        // {
        //     return num;
        // }
        // return dp[0][0];

        //3rd solution using nCr
        int N=n+m-2;
        // take what you want to take r=m-1 or r=n-1;
        int r=m-1;
        double res=1;
        for(int i=1;i<=r;i++)
        {
            res=res*(N-r+i)/i;
        }
        return (int)res;
    }
};
7200