Java Dynamic Programming Program · OT55
Unique Paths in a Grid in Java
You can reach a cell only from the cell above or the cell to its left, so the number of paths to it is the sum of the paths to those two cells.
Problem Statement
A robot starts at the top-left cell of an m × n grid and must reach the bottom-right cell, moving only right or down. Count the different paths.
Every grid has at least one path. The count is returned as a long.
Input/Output Examples
m = 3, n = 3Output6Explanation: 2 downs and 2 rights in any order: 4! / (2! × 2!).m = 1, n = 5Output1Explanation: only right moves are possible.m = 10, n = 10Output48620Explanation: C(18, 9).Visual Explanation
| c0 | c1 | c2 | |
|---|---|---|---|
| r0 | 1 | 1 | 1 |
| r1 | 1 | 2 | 3 |
| r2 | 1 | 3 | 6 |
Each row depends only on the previous row, so one array of length n, updated in place, is enough.
Approaches
The DP builds the table; the formula counts arrangements of moves directly.
Approach 1: One-Row DP (Recommended)
Idea: keep one row of path counts; for each new row, row[c] += row[c - 1].
- Fill the row with 1s (the first row).
- For each later row, update each column from left to right.
- Return the last value.
Time: O(m × n) · Space: O(n).
Approach 2: Combinatorics
Idea: every path is m - 1 downs and n - 1 rights in some order, so the answer is C(m + n - 2, m - 1).
- Let
total = m + n - 2andk = min(m - 1, n - 1). - Compute the binomial coefficient by multiplying and dividing step by step.
- Each intermediate value is an exact integer.
Time: O(min(m, n)) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: One-Row DP (Recommended)
Before the update, row[c] holds the count from above; after it, the left count has been added.
public class PathsDp {
// paths(r, c) = paths from above + paths from the left; one row of the table is enough
static long uniquePaths(int m, int n) {
long[] row = new long[n];
java.util.Arrays.fill(row, 1); // first row: only one way (keep going right)
for (int r = 1; r < m; r++) {
for (int c = 1; c < n; c++) {
row[c] += row[c - 1];
}
}
return row[n - 1];
}
public static void main(String[] args) {
int[][] inputs = {{3, 3}, {1, 5}, {10, 10}};
for (int[] in : inputs) {
System.out.println(in[0] + " x " + in[1] + " -> " + uniquePaths(in[0], in[1]));
}
}
}
3 x 3 -> 6 1 x 5 -> 1 10 x 10 -> 48620
Time complexity: O(m × n) · Space complexity: O(n).
Approach 2: Combinatorics
Fast, but intermediate products can overflow long for very large grids.
public class PathsCombinatorics {
// Every path is (m - 1) downs and (n - 1) rights in some order: C(m + n - 2, m - 1)
static long uniquePaths(int m, int n) {
int total = m + n - 2;
int k = Math.min(m - 1, n - 1);
long result = 1;
for (int i = 1; i <= k; i++) {
result = result * (total - k + i) / i; // exact at every step
}
return result;
}
public static void main(String[] args) {
int[][] inputs = {{3, 3}, {1, 5}, {10, 10}};
for (int[] in : inputs) {
System.out.println(in[0] + " x " + in[1] + " -> " + uniquePaths(in[0], in[1]));
}
}
}
3 x 3 -> 6 1 x 5 -> 1 10 x 10 -> 48620
Time complexity: O(min(m, n)) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| One-Row DP | O(m × n) | O(n) |
| Combinatorics | O(min(m, n)) | O(1) |
Here m and n are the grid dimensions. The DP fills m × n cells; the formula needs min(m, n) multiplications.
Interview Tips and Recommended Approach
- Initialise the first row and column to 1. There is only one way to move along an edge.
- Know the obstacle variant. Cells with obstacles get 0, which the DP handles naturally; the formula does not.
- Explain the formula as choosing which of the
m + n - 2moves are downs. - Watch overflow. Use
BigIntegerfor very large grids.
Recommended Approach: One-Row DP
Why: it is simple, works unchanged when cells are blocked, and uses only one row of memory.