Skip to content
StackDecode
Dynamic programmingAnimated

Minimum Path Sum

Cheapest grid path moving right or down.

Time

O(m · n)

Extra space

O(n)

Use it for

Route cost minimisation.

How it works

  1. Each cell has a cost. Move only right or down from top-left to bottom-right, minimising the total cost.
  2. best(cell) = cost(cell) + min(best(above), best(left)).
  3. The first row and column only have one way in.
  4. The bottom-right value is the cheapest route. O(rows × columns).

Watch it run

Loading animation…