Minimum Path Sum

Medium#642d table

A grid holds a cost in every cell. Go from the top-left corner to the bottom-right one, stepping only right or down, and pay for every cell you stand on, both corners included. Find the smallest total. Our example: [3, 1, 5, 1], [2, 6, 4, 2], [1, 1, 3, 2], the answer is 12.

input
grid: an m × n grid of non-negative numbers.
returns
The smallest sum of the numbers on a path from the top-left cell to the bottom-right one that moves only right or down.
constraints
  • 1 ≤ m = grid.length ≤ 100
  • 1 ≤ n = grid[i].length ≤ 100
  • 0 ≤ grid[i][j] ≤ 200
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[][]} grid
 * @return {number}
 */
function minPathSum(grid) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more dynamic programming (dp) problems

Pattern “Dynamic programming (DP)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.