Skip to content

Minimum Path Sum

Solve on LeetCodeMedium
Time
Space

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Example 1:

A 3 x 3 grid with the path 1 → 3 → 1 → 1 → 1 highlighted

Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.

Example 2:

Input: grid = [[1,2,3],[4,5,6]]
Output: 12

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1≤m,n≤2001 \le m, n \le 200
  • 0≤grid[i][j]≤2000 \le \text{grid[i][j]} \le 200

Since we can only move right or down, the last step into cell (i, j) comes either from the cell above, (i - 1, j), or from the cell to the left, (i, j - 1). Whatever path is best to (i, j), its part up to that previous cell must itself be a cheapest path to that cell (otherwise we could swap in the cheaper one). So, with best(i, j) the minimum path sum ending at (i, j):

best(i,j)=grid[i][j]+min⁡(best(i−1,j), best(i,j−1)),\text{best}(i, j) = \text{grid}[i][j] + \min\big(\text{best}(i-1, j),\ \text{best}(i, j-1)\big),

where a neighbour outside the grid is simply not an option. Trying every path recursively is exponential; filling the table row by row computes each cell once, and both neighbours are always ready.

Row i only reads row i - 1 and the cells to its left in row i, so one array dp of length n is enough: before cell (i, j) is updated, dp[j] still holds best(i - 1, j) (the cell above) and dp[j - 1] already holds best(i, j - 1) (the cell to the left).

  1. Let dp be an array of n entries, all INT_MAX (“unreachable”), except dp[0] = 0 so the top-left cell starts from cost 0.
  2. For each row i, for each column j:
    • if j > 0, set dp[j] = min(dp[j], dp[j - 1]) — the cheaper of “from above” (old dp[j]) and “from the left”;
    • then dp[j] += grid[i][j].
  3. Return dp[n - 1].

In row 0, dp[j] for j > 0 is INT_MAX, so the min picks the left neighbour; in column 0 there is no left neighbour, so only “from above” is used. INT_MAX is never added to, because every min has at least one finite side.

Example 1. Each cell shows grid | best; the blue cell is being computed from its orange neighbours (above and left); the dp line is the rolling row; at the end the cheapest path is traced back in green:

  1. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 1 of 11
  2. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 2 of 11
  3. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 3 of 11
  4. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 4 of 11
  5. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 5 of 11
  6. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 6 of 11
  7. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 7 of 11
  8. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 8 of 11
  9. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 9 of 11
  10. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 10 of 11
  11. Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 11 of 11
11 steps. Swipe or scroll to move between them.
  • Time: O(m⋅n)O(m \cdot n) — each cell is processed once in O(1)O(1).
  • Space: O(n)O(n) — one row of dp (the 2-D table is not needed). Overwriting grid in place would make it O(1)O(1) extra, at the cost of destroying the input.
  • Single row or single column: only one direction ever contributes, which the INT_MAX initialisation and the j > 0 check handle.
  • 1 x 1 grid: the answer is grid[0][0].
  • Overflow: the largest possible sum is (200+200−1)×200=79 800(200 + 200 - 1) \times 200 = 79\,800, far inside int.

Think before you peek

Work out an approach on your own first — the explanation will still be here when you want it.

class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> dp(n, INT_MAX);
dp[0] = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (j > 0) dp[j] = min(dp[j], dp[j - 1]); // from above vs from the left
dp[j] += grid[i][j];
}
}
return dp[n - 1];
}
};
class Solution {
public int minPathSum(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] dp = new int[n];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (j > 0) dp[j] = Math.min(dp[j], dp[j - 1]); // from above vs from the left
dp[j] += grid[i][j];
}
}
return dp[n - 1];
}
}
from math import inf
class Solution:
def minPathSum(self, grid: list[list[int]]) -> int:
n = len(grid[0])
dp = [inf] * n
dp[0] = 0
for row in grid:
for j, cell in enumerate(row):
if j > 0:
dp[j] = min(dp[j], dp[j - 1]) # from above vs from the left
dp[j] += cell
return dp[n - 1]

Give it a try first

Attempt the problem on LeetCode — the code will still be here.