#64

Minimum Path Sum

Medium 2D grid DP

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Move only right or down from the top-left to the bottom-right of a grid of non-negative numbers. Minimise the sum of the numbers on the path.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the cheapest right/down path.

Why this works: cost to a cell = its value + the cheaper of above and left.

Example: [[1,3,1],[1,5,1],[4,2,1]]

best path 1→3→1→1→1 = 7

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using a greedy "go to the cheaper neighbour" walk — it can miss the best path.

Solve on LeetCode ↗
Complexity
  • Time: O(m·n).
  • Space: O(1) when updating the grid in place.
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon