Unique Paths
Count the routes across a grid when you may only go right or down
DSA, Medium, 2-D DP
Which structure or algorithm, and what does it cost.
A courier crosses a street grid of rows by cols corners. The courier starts at the north-west corner and must reach the south-east corner. Each move goes one corner east or one corner south. Return the number of different routes. Two routes differ if they use different corners at any step.
Examples
Constraints
- 1 <=
rows,cols<= 20. - The answer fits in a 64-bit integer.
Your answer must contain
- The simple method and why it is too slow; the better method; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(rows * cols) time and O(cols) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Think about the last move before the finish corner. Where could the courier have been one move earlier?
Hint 2 the approach
Every corner is reached from the corner above it or the corner to its left. How do the route counts of those two corners relate to this one?
Hint 3 the key step
Fill a table where each cell holds the routes that reach it. What goes in the first row and column, and is one row enough?
Walkthrough
Full walkthrough brute force to best
Brute force
Follow every route with recursion: at each corner try east, then south. The number of routes is C(rows + cols - 2, rows - 1), about 35 billion for a 20 by 20 grid, so this is far too slow.
Better method
Recursion with memory. Let routes(r, c) be the number of routes from corner (r, c) to the finish. It is routes(r + 1, c) + routes(r, c + 1), and 1 on the last row or last column. Store each answer the first time it is found. There are rows * cols corners, each solved once, so this is O(rows * cols) time and memory. The recursion goes up to rows + cols deep.
Best method
Build a table from the start. Cell (r, c) holds the number of routes from the start to that corner. The first row and the first column hold 1, because only one straight route reaches each of them. Any other corner is reached from the corner above or the corner to its left, and no route does both. So its count is the sum of those two counts. Each row needs only the row before it, so keep one list and update it in place: cell c becomes itself (the corner above) plus cell c - 1 (the corner to the left). The answer is the last cell.
Hand trace
Run on rows = 4, cols = 5. The list starts as [1, 1, 1, 1, 1], the first row. Row 2: [1, 2, 3, 4, 5]. Row 3: [1, 3, 6, 10, 15]. Row 4: [1, 4, 10, 20, 35]. The last value is 35.
Code
def count_routes(rows, cols):
row = [1] * cols # the first row: one route to each corner
for _ in range(rows - 1):
for c in range(1, cols):
row[c] += row[c - 1] # the corner above (already in row[c]) plus the corner to the left
return row[-1]Time and memory
O(rows * cols) time, one update per corner. O(cols) memory for the single list.
Common mistakes
- Starting the first row with 0 instead of 1. Every corner on the first row has one route, so zeros make the whole table 0.
- Adding the diagonal neighbour. Moves go east or south only, so only the corner above and the corner to the left feed a corner.
- Looping rows times instead of rows - 1 times over the list that already holds the first row. The result is the count for one extra row.