Problem
Section titled “Problem”There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). 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 .
Example 1:

Input: m = 3, n = 7Output: 28Example 2:
Input: m = 3, n = 2Output: 3Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:1. Right -> Down -> Down2. Down -> Down -> Right3. Down -> Right -> DownConstraints:
Solution approach
Section titled “Solution approach”Approach 1: Dynamic programming
Section titled “Approach 1: Dynamic programming”Intuition
The last move into cell (i, j) comes either from the cell above, (i - 1, j), or from the cell to the left, (i, j - 1). Those two sets of paths are disjoint, so
with every cell in the first row and first column having exactly one path (all right moves, or all down moves). Each row only needs the row above it, so one array dp of length n suffices: before the update dp[j] still holds the value from the row above, and dp[j - 1] already holds the new value to the left.
Algorithm
- Set
dptonones (the first row). - For each row
i = 1 .. m - 1, for eachj = 1 .. n - 1:dp[j] += dp[j - 1]. (dp[0]stays1, the first column.) - Return
dp[n - 1].
Complexity
- Time: — one addition per cell.
- Space: — a single row.
Approach 2: Combinatorics
Section titled “Approach 2: Combinatorics”Intuition
The table above is Pascal’s triangle turned on its side, and there is a direct reason for that. Every path makes exactly m - 1 down moves and n - 1 right moves, m + n - 2 moves in total, in some order. A path is completely determined by which of those positions hold the down moves, so the answer is
Choosing the smaller of the two (k = min(m, n) - 1) means only k multiplications.
Algorithm
- Let
total = m + n - 2andk = min(m, n) - 1. - Start with
ans = 1. Fori = 1 .. k, setans = ans * (total - k + i) / i. - After step
i,ansequals , which is an integer, so each division is exact. Returnans.
Complexity
- Time: —
kiterations. - Space: .
Edge cases
Section titled “Edge cases”- A single row or column (
m = 1orn = 1): Approach 1 never enters the loop and returnsdp[n - 1] = 1; in Approach 2k = 0, soansstays1. - Overflow: the problem guarantees the answer is at most . In Approach 1 every cell is at most the answer (values only grow towards the bottom-right). In Approach 2 the running value is at most the answer before multiplying by a factor of at most , so
long longholdsans * (total - k + i)comfortably.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: int uniquePaths(int m, int n) { vector<int> dp(n, 1); // dp[j] = paths to column j of the current row for (int i = 1; i < m; i++) for (int j = 1; j < n; j++) dp[j] += dp[j - 1]; return dp[n - 1]; }};class Solution { public int uniquePaths(int m, int n) { int[] dp = new int[n]; // dp[j] = paths to column j of the current row Arrays.fill(dp, 1); for (int i = 1; i < m; i++) for (int j = 1; j < n; j++) dp[j] += dp[j - 1]; return dp[n - 1]; }}class Solution: def uniquePaths(self, m: int, n: int) -> int: dp = [1] * n # dp[j] = paths to column j of the current row for _ in range(1, m): for j in range(1, n): dp[j] += dp[j - 1] return dp[-1]class Solution { public: int uniquePaths(int m, int n) { int total = m + n - 2; int k = min(m, n) - 1; long long ans = 1; for (int i = 1; i <= k; i++) ans = ans * (total - k + i) / i; // now ans == C(total - k + i, i) return (int)ans; }};class Solution { public int uniquePaths(int m, int n) { int total = m + n - 2; int k = Math.min(m, n) - 1; long ans = 1; for (int i = 1; i <= k; i++) ans = ans * (total - k + i) / i; // now ans == C(total - k + i, i) return (int) ans; }}class Solution: def uniquePaths(self, m: int, n: int) -> int: total = m + n - 2 k = min(m, n) - 1 ans = 1 for i in range(1, k + 1): ans = ans * (total - k + i) // i # now ans == C(total - k + i, i) return ansGive it a try first
Attempt the problem on LeetCode — the code will still be here.







