Unique Paths
Count the ways a robot can cross a grid from the top-left cell to the bottom-right cell, moving only right or down.
A robot stands in the top-left cell of a grid with mn
Example 1
Output: 28
Example 2
Output: 3
Explanation: right-down-down, down-right-down, down-down-right.
The Idea
Picture a board of squares, like a small chessboard, with a toy robot on the top-left square. Each square of the board is a cell of the grid. The robot can only roll right or down, so every trip ends sooner or later on the bottom-right cell. The question is how many different trips there are.
Example 2 is small enough to list them all. The board has 3 rows and 2 columns, so the robot must go down twice and right once. The one move right can come first, second or last, which gives right-down-down, down-right-down and down-down-right: three paths.
Example 1 has 3 rows and 7 columns, and the answer is 28. Writing out 28 paths by hand is already tiring, and bigger boards have many thousands. So how can we count the paths without listing them?
Think about the robot's very last move. It enters the bottom-right cell either from the cell above it or from the cell to its left. A path that ends with a move down cannot also end with a move right, so the two groups never share a path. The number of paths to the bottom-right cell is the number of paths to the cell above plus the number of paths to the cell on the left.
The same holds for every cell, so we can write a count into each one. Look at the picture below.
The grey cells in the top row and the left column all hold 1. The robot reaches a cell in row 0 by rolling straight right, and a cell in column 0 by rolling straight down, with no other choice. Every other cell adds the cell above it to the cell on its left. In row 1, column 1 gets 1 + 1 = 2, column 2 gets 1 + 2 = 3, and so on up to 7.
Now look at the finish, in row 2 and column 6. The cell above it, marked "from above: 7 paths", holds 7. The cell on its left, marked "from the left: 21 paths", holds 21. Together they make 28, the answer to Example 1.
So no path is ever walked. Each cell's count is built from two counts already written into the grid, row by row from the top. Building the count for the finish out of the counts of the cells before it, as this grid does, is called dynamic programming.
Solution Approach
Turning the grid of counts into code means settling three things: why walking every path is too slow, what each cell of the table stands for, and how to keep only one row of it. There is also a formula that gives the count directly, as an optional extra.
Walking every path
Why not let a program try every path, moving right or down from each cell, and count the ones that reach the finish? It gives the right answer, but it has to arrive at the finish once for each path. A 10 by 10 grid has 48,620 paths, and each one is 18 moves long, so the work grows as fast as the answer does.
What one cell of the table means
Which number goes into the cell in row ijpaths(i, j) = paths(i - 1, j) + paths(i, j - 1)
Filling row by row from the top, and each row from left to right, means both of those counts are ready when a cell needs them. The last cell filled is the finish.
Keeping one row instead of the whole grid
Does the program need all mndpdp
To fill the cell in column jdp[j - 1]dp[j]dp[j]dp[j - 1]dp[6]
A formula for the count (optional)
Every path in Example 1 is 8 moves long: 2 moves down and 6 moves right, in some order. A path is fixed by choosing which 2 of the 8 moves go down, and there are 28 ways to choose them. In general the answer is (m + n - 2)! / ((m - 1)! × (n - 1)!), where k! means 1 × 2 × ... × k.
The formula is correct, but computing the factorials in Java intintintint
Java Solution
The method uniquePaths
- First, create the array
withdp , one count per column. It holds one row of the grid at a time.new int[n] - Fill it with 1 using
. That is the top row, where each cell has one path.Arrays.fill(dp, 1) - Go through the remaining rows with a loop that moves
from 1 toi . Inside it, a second loop movesm - 1 from 1 toj and runsn - 1 , adding the count on the left to the count from above. The second loop starts at column 1, sodp[j] += dp[j - 1] stays 1 for the left column.dp[0] - After the last row,
holds the count for the bottom-right cell, and the method returns it.dp[n - 1]
The main
import java.util.Arrays;
public class UniquePaths {
public static 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); // the top row: one way to each cell
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[j] += dp[j - 1]; // from above (old dp[j]) plus from the left
}
}
return dp[n - 1];
}
public static void main(String[] args) {
System.out.println("3 x 7: " + uniquePaths(3, 7));
System.out.println("3 x 2: " + uniquePaths(3, 2));
System.out.println("1 x 1: " + uniquePaths(1, 1));
System.out.println("1 x 5: " + uniquePaths(1, 5));
System.out.println("5 x 1: " + uniquePaths(5, 1));
System.out.println("10 x 10: " + uniquePaths(10, 10));
}
}
Running it prints:
3 x 7: 28
3 x 2: 3
1 x 1: 1
1 x 5: 1
5 x 1: 1
10 x 10: 48620
The line 3 x 7: 283 x 2: 31 x 1: 1
The lines 1 x 5: 15 x 1: 1ijdp[0]10 x 10: 48620int
Complexity
The work is one addition for every cell outside the top row and the left column.
- Time: O(m × n), because the two loops visit each of the (m - 1) × (n - 1) inner cells once, with one addition each, after the fill of n counts for the top row.
- Space: O(n), because
keeps only one row of n counts, and each new row is written over the row above it.dp
When some cells are blocked and the robot must go around them, the same table still works with one change: see