Problem
Section titled “Problem”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:

Input: grid = [[1,3,1],[1,5,1],[4,2,1]]Output: 7Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.Example 2:
Input: grid = [[1,2,3],[4,5,6]]Output: 12Constraints:
m == grid.lengthn == grid[i].length
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”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):
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).
Algorithm
Section titled “Algorithm”- Let
dpbe an array ofnentries, allINT_MAX(“unreachable”), exceptdp[0] = 0so the top-left cell starts from cost0. - For each row
i, for each columnj:- if
j > 0, setdp[j] = min(dp[j], dp[j - 1])— the cheaper of “from above” (olddp[j]) and “from the left”; - then
dp[j] += grid[i][j].
- if
- 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.
Walkthrough
Section titled “Walkthrough”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:
Complexity
Section titled “Complexity”- Time: — each cell is processed once in .
- Space: — one row of
dp(the 2-D table is not needed). Overwritinggridin place would make it extra, at the cost of destroying the input.
Edge cases
Section titled “Edge cases”- Single row or single column: only one direction ever contributes, which the
INT_MAXinitialisation and thej > 0check handle. 1 x 1grid: the answer isgrid[0][0].- Overflow: the largest possible sum is , 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.
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 1 of 11](/CodePrime/img/codes/minimum-path-sum/dp/01.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 2 of 11](/CodePrime/img/codes/minimum-path-sum/dp/02.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 3 of 11](/CodePrime/img/codes/minimum-path-sum/dp/03.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 4 of 11](/CodePrime/img/codes/minimum-path-sum/dp/04.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 5 of 11](/CodePrime/img/codes/minimum-path-sum/dp/05.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 6 of 11](/CodePrime/img/codes/minimum-path-sum/dp/06.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 7 of 11](/CodePrime/img/codes/minimum-path-sum/dp/07.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 8 of 11](/CodePrime/img/codes/minimum-path-sum/dp/08.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 9 of 11](/CodePrime/img/codes/minimum-path-sum/dp/09.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 10 of 11](/CodePrime/img/codes/minimum-path-sum/dp/10.webp)
![Minimum path sum DP on [[1,3,1],[1,5,1],[4,2,1]], step 11 of 11](/CodePrime/img/codes/minimum-path-sum/dp/11.webp)