304 North Cardinal St.
Dorchester Center, MA 02124

# Unique Paths LeetCode Solution

## Problem – Unique Paths LeetCode Solution

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

Given the two integers `m` and `n`, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The test cases are generated so that the answer will be less than or equal to `2 * 109`.

Example 1:

``````Input: m = 3, n = 7
Output: 28
``````

Example 2:

``````Input: m = 3, n = 2
Output: 3
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down
``````

Constraints:

• `1 <= m, n <= 100`

### Unique Paths LeetCode Solution in Java

``````public class Solution {
public int uniquePaths(int m, int n) {
if(m == 1 || n == 1)
return 1;
m--;
n--;
if(m < n) {              // Swap, so that m is the bigger number
m = m + n;
n = m - n;
m = m - n;
}
long res = 1;
int j = 1;
for(int i = m+1; i <= m+n; i++, j++){       // Instead of taking factorial, keep on multiply & divide
res *= i;
res /= j;
}

return (int)res;
}
}
``````

### Unique Paths LeetCode Solution in C++

``````class Solution {
public:
int uniquePaths(int m, int n) {
int N = n+m-2; // total steps = n-1 + m-1
int r = min(n,m) - 1; // will iterate on the minimum for efficiency = (total) C (min(right, down))

double res = 1;

// compute nCr
for(int i=1; i<=r; ++i, N--){

res = res * (N) / i;
}

return (int)res;
}
};
``````

### Unique Paths LeetCode Solution in Python

``````class Solution:
# @return an integer
def uniquePaths(self, m, n):
aux = [[1 for x in range(n)] for x in range(m)]
for i in range(1, m):
for j in range(1, n):
aux[i][j] = aux[i][j-1]+aux[i-1][j]
return aux[-1][-1]
``````
##### Unique Paths LeetCode Solution Review:

In our experience, we suggest you solve this Unique Paths LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Unique Paths LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Unique Paths LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions