101 Logo
onenoughtone

Problem Statement

Minimum Path Sum

Given a m x n grid filled with non-negative integers, 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.

Examples

Example 1:

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
Explanation: The path 1 → 2 → 3 → 6 minimizes the sum.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 200
  • 0 <= grid[i][j] <= 100

Problem Breakdown

To solve this problem, we need to:

  1. This problem can be solved using dynamic programming.
  2. For each cell, the minimum path sum is the value of the cell plus the minimum of the path sums from the cell above and the cell to the left.
  3. The base cases are the top-left cell, the first row, and the first column.
  4. We can use a 2D DP array to store the minimum path sum for each cell.
  5. We can also optimize the space complexity by using a 1D DP array, as we only need the values from the current row and the row above.
ProblemSolutionCode
101 Logo
onenoughtone