Unique Paths
Solve this ProblemTest Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ m, n ≤ 100
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
Approach & Solutions
Brute Force — Recursive Without Memoization
BruteStanding on any cell, the only two legal moves are one step down or one step right, so the number of ways to reach the bottom-right corner from that cell is just the number of ways from moving down plus the number of ways from moving right. Reaching the bottom-right corner itself counts as one complete path; stepping off the bottom or right edge of the grid is not a valid move and contributes nothing. Starting the recursion at the top-left corner and exploring every down/right choice at every cell counts every distinct path, though the same cell ends up being visited — and its sub-count recomputed — along many different paths.
O(2^(m+n))O(m + n)1class Solution {
2 private int m;
3 private int n;
4
5 public int uniquePaths(int m, int n) {
6 this.m = m;
7 this.n = n;
8 return solve(0, 0);
9 }
10
11 private int solve(int i, int j) {
12 if (i == m - 1 && j == n - 1) return 1;
13 if (i >= m || j >= n) return 0;
14 return solve(i + 1, j) + solve(i, j + 1);
15 }
16}Optimal — Bottom-Up 1D DP
OptimalTrack, for the row currently being processed, the number of ways to reach each column from the top-left corner. Every cell in the very first row only has one way to be reached — moving right the whole way — so that row starts as all 1s. For every later row, the count at a column is the count already sitting there from the row above (arriving by moving down) plus the count just computed at the column to its left in this same row (arriving by moving right) — and since the array is updated left to right, that left-neighbor value is already the current row's, not the row above's. After sweeping through every row, the last column holds the total number of paths.
O(m × n)O(n)1class Solution {
2 public int uniquePaths(int m, int n) {
3 int[] dp = new int[n];
4 Arrays.fill(dp, 1);
5 for (int i = 1; i < m; i++) {
6 for (int j = 1; j < n; j++) {
7 dp[j] = dp[j] + dp[j - 1];
8 }
9 }
10 return dp[n - 1];
11 }
12}