AlgoFrames

Minimum path sum

Standard · Write code · about 12 min

grid holds non-negative costs. Start in the top-left cell, move only right or down, and finish in the bottom-right cell. The cost of a path is the sum of every cell on it, both ends included.

Return the smallest possible cost.

Example 1

Input:

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

Output:

7

1 + 3 + 1 + 1 + 1 = 7 along the drawn path.

Example 2

Input:

[[[1,2,3],[4,5,6]]]

Output:

12

1 → 2 → 3 → 6.

Constraints

  • 1 ≤ rows, cols ≤ 200
  • 0 ≤ grid[r][c] ≤ 100

Learn the idea first: Grid DP: paths, minimum path sum, obstacles, a visual lesson.

More Grid DP: paths, minimum path sum, obstacles problems