Skip to content

Minimum Path Sum

MediumDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

Each cell of an m × n grid holds a non-negative cost. Find a path from the top-left to the bottom-right cell, moving only right or down, that has the smallest sum of the cells it visits (including both corners). Return that sum.

grid = [[1, 3, 1],
        [1, 5, 1],
        [4, 2, 1]]   →  7   (1 → 3 → 1 → 1 → 1)
 
grid = [[1, 2, 3],
        [4, 5, 6]]   →  12

Constraints

  • 1 <= m, n <= 200 where the grid is m × n
  • 0 <= grid[r][c] <= 200