AlgoMaster Logo

Minimum Path Sum

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Recursive Approach

Intuition:

The recursive approach is the most straightforward way to tackle this problem. Imagine that you need to move from the top-left to the bottom-right of the grid. At each cell (i, j), you have the choice to move either right to (i, j+1) or down to (i+1, j). The recursive solution explores both these paths and keeps track of the minimum sum encountered.

Code:

2. Dynamic Programming Approach (Bottom-Up)

Intuition:

This approach builds on the recursive solution but uses dynamic programming to avoid repeated calculations. The idea is to work backwards from the destination (bottom-right) cell and fill in a table with the minimum path sum to reach the bottom-right from any given cell.

Code:

3. Dynamic Programming Approach (In-Place)

Intuition:

We observe that to compute the value at any cell (i, j), we only need the values of the cell directly to the right (i, j+1) and the cell directly below (i+1, j) in the dp table. This allows us to actually use the input grid itself as our dp table to save space.

Code: