The minimum path sum of a grid is the smallest total you can collect while walking from the top-left cell to the bottom-right cell when every step must move either one cell right or one cell down. This is LeetCode 64, a classic first taste of dynamic programming: the cheapest way to reach any cell is built from the cheapest ways to reach the two cells that feed into it. You are given a rectangular grid of non-negative numbers and return that minimum sum — the number itself, not the path that produces it.
minPathSum(grid) // grid: number[][], non-empty, non-negative -> number
// The classic 3x3: the path 1 -> 3 -> 1 -> 1 -> 1 collects the least, 7.
minPathSum([[1, 3, 1], [1, 5, 1], [4, 2, 1]]); // 7
// A single row has only one possible path, so you sum every cell.
minPathSum([[1, 2, 3]]); // 6
// A 2x2 grid: drop straight down then right (1 -> 1 -> 1) and skip the 2.
minPathSum([[1, 2], [1, 1]]); // 3
(i, j) you may step to (i + 1, j) or (i, j + 1). No diagonals, and never up or left.>= 0. Values can repeat, and a grid of all zeros costs 0.1x1 grid's answer is simply its single cell.grid, since a caller may reuse it.