D · Expert · 12 min

Grid Paths: Counts and Min Sums

Right-and-down grids are DAGs. Unique paths add the cell above and the cell to the left; min path sum takes the min of those two, then adds the cell.

An m × n grid. You start at the top-left and may move only right or down to the bottom-right. Two classic questions:

1. Unique paths — how many ways? (often empty cells, sometimes with rocks) 2. Min path sum — each cell has a positive cost; minimize the sum along the path.

Both are DAG DP. Every cell has a unique topological order (by r + c, or just nested loops r then c). There is no cycle, so you never need a shortest-path algorithm. BFS would count edges, not sums or combinations.

Unique paths, no obstacles. First row and first column are 1.js
function uniquePaths(m, n) {
  const dp = Array.from({ length: m }, () => Array(n).fill(0));
  for (let r = 0; r < m; r++) dp[r][0] = 1;
  for (let c = 0; c < n; c++) dp[0][c] = 1;
  for (let r = 1; r < m; r++) {
    for (let c = 1; c < n; c++) {
      dp[r][c] = dp[r - 1][c] + dp[r][c - 1];
    }
  }
  return dp[m - 1][n - 1];
}

You must take exactly (m-1) downs and (n-1) rights, in some order. If the grid is empty, that is the binomial C(m+n-2, m-1). DP still matters: obstacles kill the closed form, and min path sum is not a count.

Min path sum recurrence:

dp[r][c] = grid[r][c] + min(from above, from left), with the missing neighbor treated as absent (first row has no above; first column has no left).

Min path sum. Mutating a copy of the grid is legal.js
function minPathSum(grid) {
  const m = grid.length;
  const n = grid[0].length;
  const dp = grid.map((row) => row.slice());
  for (let r = 0; r < m; r++) {
    for (let c = 0; c < n; c++) {
      if (r === 0 && c === 0) continue;
      const up = r > 0 ? dp[r - 1][c] : Infinity;
      const left = c > 0 ? dp[r][c - 1] : Infinity;
      dp[r][c] = grid[r][c] + Math.min(up, left);
    }
  }
  return dp[m - 1][n - 1];
}

TRACE

grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]]. Fill min-path dp, row-major. Cells listed row 0, then 1, then 2.

(0,0)

Start. dp[0][0] = 1.

01
13
21
31
45
51
64
72
81

1 . . / . . . / . . .

1 / 5

CHECK

Why is the recurrence only 'from above' and 'from left', not from below or right?

CHECK

An empty 2×3 grid (2 rows, 3 columns). How many unique paths, and why not 2×3=6?

Checks 0/2

Next lesson