All topics
113

Filling a grid where each cell comes from its top and its left

Tier 1Locked

Where it runs

1. The problem

A robot stands in the top-left corner of a warehouse grid and has to reach the bottom-right corner. It moves only right or down, one cell per move. Planning raises two questions. How many different routes are there? And which route is the cheapest, if crossing each cell costs something (the aisle is crowded, or there is a slope)?

Trying the routes by hand soon becomes impossible. An 8 by 8 grid already has 3432 of them, and a real warehouse plan has far more cells.

In the previous lesson you filled one row of cells. Here the answer depends on two coordinates, the row and the column, so the row turns into a grid of cells. The idea stays the same: you fill each cell from cells you have already filled.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included

Recognise it

Dynamic programming (1-D)

Say these words and it is this pattern

  • best/maximum/minimum ending at position i
  • ways to reach, climb, or tile
  • cannot pick two adjacent items
  • minimum cost path through a grid
  • overlapping subproblems, same state recomputed

Looks similar, isn't

n states with O(1) work each gives O(n). Memory drops to O(1) when only the last states matter.

On LeetCode

Grid problems where you move only right or down and want the number of routes or the cheapest route. Each cell comes from the cell above and the cell to the left, so one pass row by row fills the table.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy