#62 Unique Paths
frequently reported atAmazonBloombergMetaGoogleGoldman Sachs
the problem
In plain words
A robot starts at the top-left of an m × n grid and moves only right or down. How many different paths reach the bottom-right corner?
in m = 3, n = 7
out 28
in m = 3, n = 2
out 3
R→D→D, D→D→R, D→R→D.
- 1 ≤ m, n ≤ 100
- The answer is ≤ 2·10⁹
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternGrid DPMath shortcut (closed form)
Every path into a cell arrives from above or from the left, so paths(r, c) = paths(r−1, c) + paths(r, c−1). Top row and left column have exactly one path.
solutions
From brute force to optimal
paths(r, c) = paths(r−1, c) + paths(r, c−1).
- Edge (r = 0 or c = 0) → 1.
- Sum the two sub-calls.
Two branches per step, path length m + n − 2.
Recursion depth.
class Solution { public int uniquePaths(int m, int n) { return paths(m - 1, n - 1); } /** Paths from (0,0) to (r,c) moving only right/down. */ private int paths(int r, int c) { if (r == 0 || c == 0) return 1; // top row / left column: one straight path return paths(r - 1, c) + paths(r, c - 1); // arrived from above or from the left }}Cache paths(r, c) in an m × n array (0 = unknown; real answers ≥ 1).
- Check memo, compute, store.
Each cell once.
Memo + stack.
class Solution { public int uniquePaths(int m, int n) { return paths(m - 1, n - 1, new int[m][n]); } private int paths(int r, int c, int[][] memo) { if (r == 0 || c == 0) return 1; if (memo[r][c] != 0) return memo[r][c]; return memo[r][c] = paths(r - 1, c, memo) + paths(r, c - 1, memo); }}Fill row by row, left to right.
- Edges = 1.
- dp[r][c] = dp[r−1][c] + dp[r][c−1].
One O(1) step per cell.
The table.
class Solution { public int uniquePaths(int m, int n) { int[][] dp = new int[m][n]; // dp[r][c] = paths that reach (r, c) for (int r = 0; r < m; r++) { for (int c = 0; c < n; c++) { if (r == 0 || c == 0) dp[r][c] = 1; // edges: only one way in else dp[r][c] = dp[r - 1][c] + dp[r][c - 1]; // from above + from left } } return dp[m - 1][n - 1]; }}Raise the landscape
dp[c] += dp[c−1]: old dp[c] is "from above", dp[c−1] is "from the left".
- dp = all 1s (top row).
- For each next row: for c = 1..n−1: dp[c] += dp[c−1].
Same updates.
One row.
class Solution { public int uniquePaths(int m, int n) { int[] dp = new int[n]; // dp[c] = paths into column c of the current row Arrays.fill(dp, 1); // the top row for (int r = 1; r < m; r++) { for (int c = 1; c < n; c++) { dp[c] += dp[c - 1]; // old dp[c] = from above, dp[c-1] = from left (already this row) } } return dp[n - 1]; }}One row does the whole grid
A path is m−1 downs and n−1 rights in some order: C(m+n−2, m−1).
- Multiply and divide step by step in a long so every intermediate value is an exact integer.
m − 1 multiply/divide steps (pick the smaller of m, n for O(min(m, n))).
One long.
class Solution { /** Any path is (m-1) downs and (n-1) rights in some order: choose where the downs go. */ public int uniquePaths(int m, int n) { long result = 1; int downs = m - 1, total = m + n - 2; for (int k = 1; k <= downs; k++) { result = result * (total - downs + k) / k; // stays an exact integer at every step } return (int) result; }}in the room
How to say it in the interview
talk track
- Give the DP first (it generalises to obstacles), then mention the closed form as a bonus.
traps
- Computing factorials directly overflows even a long (21! already does). Multiply-then-divide incrementally instead.
what they ask next
Follow-ups
Obstacles (LC 63)?
Same DP; an obstacle cell is 0. The combinatorics shortcut no longer works. See Unique Paths II below.
Min cost path?
That's Minimum Path Sum (LC 64): min instead of +.
before you submit
Edge cases to test
- 1 × 1 → 1 (already at the goal).
- 1 × n or m × 1 → 1 straight path.
- 17 × 18 → 1,166,803,110: fine for the DP, but the factorial formula needs 33!, which overflows a long.
practise next
Similar problems
- #63Unique Paths IIMediumin Dynamic Programming
- #64Minimum Path SumMediumin Dynamic Programming
- #120TriangleMediumin Dynamic Programming