Cheapest Right-Down Route Through a Cost Grid
Dynamic ProgrammingAuto-graded · 16 test casesLast updated
Compute the minimum total cost of a route from the top-left to the bottom-right of a grid when only right and down moves are allowed, using dynamic programming.
Cheapest Right-Down Route
You are given a rectangular grid with m rows and n columns. Every cell holds a non-negative integer, which is the cost of stepping on that cell.
A route begins at the top-left cell (0, 0) and ends at the bottom-right cell (m-1, n-1). From any cell you may move exactly one step right or exactly one step down; no other moves are permitted. The cost of a route is the sum of the values of every cell it visits, including both the starting cell and the final cell.
Return the smallest possible cost over all valid routes.
For instance, in the grid [[1, 3, 1], [1, 5, 1], [4, 2, 1]] the cheapest route visits cells with values 1, 3, 1, 1, 1, so the answer is 7.
Input
grid: a list ofmlists, each containingnnon-negative integers.
Output
- A single integer: the minimum route cost.
Notes
- A grid with a single cell has exactly one route, whose cost is that cell's value.
- In a grid with one row or one column, the route is forced.
Constraints
1 <= m, n <= 200- The grid is non-empty and rectangular (all rows have the same length).
- Every cell value is a non-negative integer.
Examples
Example 1
Input: cheapest_route_cost([[1, 3, 1], [1, 5, 1], [4, 2, 1]])
Output: 7
Hints
Hint 1
Think about the last step of any route: the final cell is entered either from the cell above it or from the cell to its left. What does that say about the best cost of reaching it?
Hint 2
Define best[r][c] as the cheapest cost to arrive at cell (r, c). Then best[r][c] = grid[r][c] + min(best[r-1][c], best[r][c-1]), with only one available predecessor along the first row and first column.
Hint 3
Cells are processed row by row, so you only need the previous row's values. A single array of length n, updated in place as you sweep each row, is enough.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Because moves go only right or down, any route reaches a cell (r, c) either from (r-1, c) above or from (r, c-1) on its left. So the cheapest cost to reach (r, c) is the cell's own value plus the smaller of the cheapest costs of those two predecessors:
best[r][c] = grid[r][c] + min(best[r-1][c], best[r][c-1])
Boundary cases:
- The start cell costs just its own value.
- In the first row, the only predecessor is the left cell.
- In the first column, the only predecessor is the cell above.
Only the previous row is ever needed, so one array of length n suffices. When we visit (r, c), best[c] still holds the value for the cell above (row r-1), and best[c-1] has already been updated for the current row, so it holds the value for the left cell. After the last row, best[-1] is the answer.
Why it is correct
The problem has optimal substructure: an optimal route to (r, c) must consist of an optimal route to one of its predecessors plus the current cell. Processing in row-major order guarantees both predecessors are finalized before the current cell is computed. By induction over the cells, every stored value is the true minimum, so the final value is the minimum for the bottom-right cell.
Complexity
- Time:
O(m * n), one constant-time update per cell. - Space:
O(n)extra, and the input is not modified.
Reference solution
from typing import List
def cheapest_route_cost(grid: List[List[int]]) -> int:
rows, cols = len(grid), len(grid[0])
best = [0] * cols
for r in range(rows):
for c in range(cols):
if r == 0 and c == 0:
best[c] = grid[r][c]
elif r == 0:
best[c] = best[c - 1] + grid[r][c]
elif c == 0:
best[c] += grid[r][c]
else:
best[c] = min(best[c], best[c - 1]) + grid[r][c]
return best[-1]