Lesson 1 · Unit 03
Grid paths
Count routes cell by cell, with walls and a rolling row
What you practise
- Unique paths with obstacles
- The shared-row trap when creating 2D arrays
Try a question from this lesson
Pick one
A robot moves only right or down through a grid. dp[r][c] counts the routes that reach row r, column c. Which rule fills a cell?
Correct answer
dp[r - 1][c] + dp[r][c - 1]
Why
The last move came from the cell above or the cell to the left, and those two sets of routes never overlap, so the counts add. max would pick one group and lose the other; the cells below and to the right are not filled yet.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A table with two indexes
The state is a position: a row and a column. Along the top row and the left column there is only one route (straight along the edge), so those cells start at 1.
Every other cell adds the cell above and the cell to its left. Filling row by row means both are ready in time.
def paths(rows, cols):
dp = [[1] * cols for _ in range(rows)]
for r in range(1, rows):
for c in range(1, cols):
up = dp[r - 1][c]
dp[r][c] = up + dp[r][c - 1]
return dp[-1][-1]function paths(rows, cols) {
const dp = [];
for (let r = 0; r < rows; r++) {
dp.push(Array(cols).fill(1));
}
for (let r = 1; r < rows; r++) {
for (let c = 1; c < cols; c++) {
const up = dp[r - 1][c];
dp[r][c] = up + dp[r][c - 1];
}
}
return dp[rows - 1][cols - 1];
}function paths(
rows: number,
cols: number,
): number {
const dp: number[][] = [];
for (let r = 0; r < rows; r++) {
dp.push(Array<number>(cols).fill(1));
}
for (let r = 1; r < rows; r++) {
for (let c = 1; c < cols; c++) {
const up = dp[r - 1][c];
dp[r][c] = up + dp[r][c - 1];
}
}
return dp[rows - 1][cols - 1];
}Python noteThe list comprehension builds a separate row list on every pass. dp[-1][-1] is the last cell of the last row.
TypeScript notenumber[][] is an array of number arrays: a grid. An empty [] needs the annotation, because there is nothing to infer the element type from.
Concept
One row is enough
A cell reads only the row above and its own row. So keep a single row and update it in place.
Just before row[c] is updated, it still holds the value from the row above. row[c - 1] was updated a moment ago, so it is the cell to the left. Adding them is the same rule in O(cols) memory.
def paths(rows, cols):
row = [1] * cols
for _ in range(1, rows):
for c in range(1, cols):
row[c] += row[c - 1]
return row[-1]function paths(rows, cols) {
const row = Array(cols).fill(1);
for (let r = 1; r < rows; r++) {
for (let c = 1; c < cols; c++) {
row[c] += row[c - 1];
}
}
return row[cols - 1];
}function paths(
rows: number,
cols: number,
): number {
const row = Array<number>(cols).fill(1);
for (let r = 1; r < rows; r++) {
for (let c = 1; c < cols; c++) {
row[c] += row[c - 1];
}
}
return row[cols - 1];
}Python noteThe loop variable _ is a convention for "not used": the row number itself never appears in the update.
TypeScript noteArray<number>(cols) sets the element type, so fill(1) gives number[] and not any[].
10 more exercises in the app
- 4× type the answer
- 2× multiple choice
- 2× fill the blanks
- 1× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.