There are 3 main approaches to solve this problem:
The key insight for solving this problem is to use dynamic programming to build up the solution from smaller subproblems.
Let's define dp[i][j]
as the minimum path sum to reach the cell at position (i, j)
from the top-left corner (0, 0)
.
Since we can only move right or down, to reach the cell (i, j)
, we must come from either the cell above it (i-1, j)
or the cell to its left (i, j-1)
. Therefore, the recurrence relation is:
dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])
For the base cases:
dp[0][0] = grid[0][0]
(the top-left cell)i = 0
), dp[0][j] = grid[0][j] + dp[0][j-1]
(we can only come from the left)j = 0
), dp[i][0] = grid[i][0] + dp[i-1][0]
(we can only come from above)The final answer is dp[m-1][n-1]
, which represents the minimum path sum to reach the bottom-right corner.
We need to fill in the DP table of size m × n, and each cell takes constant time to compute.
We use a 2D DP array of size m × n to store the minimum path sum for each cell.
We can optimize the space complexity of the dynamic programming approach by using a 1D array instead of a 2D array.
The key observation is that when we're calculating dp[i][j]
, we only need the values from the current row and the row above it. Specifically, we need dp[i-1][j]
and dp[i][j-1]
.
If we use a 1D array dp
where dp[j]
represents the minimum path sum to reach the cell at position (i, j)
, then:
dp[j]
represents the minimum path sum to reach the cell at position (i-1, j)
.dp[j-1]
, dp[j-1]
represents the minimum path sum to reach the cell at position (i, j-1)
.So, the recurrence relation becomes:
dp[j] = grid[i][j] + min(dp[j], dp[j-1])
We need to be careful with the first column (j = 0
), as there is no cell to its left. For the first column, we simply update dp[0] = dp[0] + grid[i][0]
.
The final answer is dp[n-1]
, which represents the minimum path sum to reach the bottom-right corner.
We still need to process each cell in the grid, which takes O(m * n) time.
We only use a 1D array of size n to store the minimum path sum for the current row.
We can further optimize the space complexity by modifying the input grid in-place to store the minimum path sum for each cell.
The recurrence relation remains the same:
grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1])
We need to be careful with the base cases:
i = 0
), grid[0][j] = grid[0][j] + grid[0][j-1]
for all j from 1 to n-1.j = 0
), grid[i][0] = grid[i][0] + grid[i-1][0]
for all i from 1 to m-1.The final answer is grid[m-1][n-1]
, which represents the minimum path sum to reach the bottom-right corner.
Note that this approach modifies the input grid, which might not be desirable in all situations. If preserving the input is important, we should use one of the previous approaches.
We still need to process each cell in the grid, which takes O(m * n) time.
We modify the input grid in-place, so we don't need any additional space beyond the input.
123456789101112131415161718192021222324function minPathSum(grid): m = grid.length n = grid[0].length // Initialize DP array dp = new 2D array of size m × n // Set base case for top-left cell dp[0][0] = grid[0][0] // Set base cases for first row for j from 1 to n-1: dp[0][j] = grid[0][j] + dp[0][j-1] // Set base cases for first column for i from 1 to m-1: dp[i][0] = grid[i][0] + dp[i-1][0] // Fill DP array for i from 1 to m-1: for j from 1 to n-1: dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]) return dp[m-1][n-1]
Understand different approaches to solve the minimum path sum problem and analyze their efficiency.
The key insight for solving this problem is to use dynamic programming to build up the solution from smaller subproblems.
Let's define dp[i][j]
as the minimum path sum to reach the cell at position (i, j)
from the top-left corner (0, 0)
.
Since we can only move right or down, to reach the cell (i, j)
, we must come from either the cell above it (i-1, j)
or the cell to its left (i, j-1)
. Therefore, the recurrence relation is:
dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])
For the base cases:
dp[0][0] = grid[0][0]
(the top-left cell)i = 0
), dp[0][j] = grid[0][j] + dp[0][j-1]
(we can only come from the left)j = 0
), dp[i][0] = grid[i][0] + dp[i-1][0]
(we can only come from above)The final answer is dp[m-1][n-1]
, which represents the minimum path sum to reach the bottom-right corner.
We can optimize the space complexity of the dynamic programming approach by using a 1D array instead of a 2D array.
The key observation is that when we're calculating dp[i][j]
, we only need the values from the current row and the row above it. Specifically, we need dp[i-1][j]
and dp[i][j-1]
.
If we use a 1D array dp
where dp[j]
represents the minimum path sum to reach the cell at position (i, j)
, then:
dp[j]
represents the minimum path sum to reach the cell at position (i-1, j)
.dp[j-1]
, dp[j-1]
represents the minimum path sum to reach the cell at position (i, j-1)
.So, the recurrence relation becomes:
dp[j] = grid[i][j] + min(dp[j], dp[j-1])
We need to be careful with the first column (j = 0
), as there is no cell to its left. For the first column, we simply update dp[0] = dp[0] + grid[i][0]
.
The final answer is dp[n-1]
, which represents the minimum path sum to reach the bottom-right corner.
We can further optimize the space complexity by modifying the input grid in-place to store the minimum path sum for each cell.
The recurrence relation remains the same:
grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1])
We need to be careful with the base cases:
i = 0
), grid[0][j] = grid[0][j] + grid[0][j-1]
for all j from 1 to n-1.j = 0
), grid[i][0] = grid[i][0] + grid[i-1][0]
for all i from 1 to m-1.The final answer is grid[m-1][n-1]
, which represents the minimum path sum to reach the bottom-right corner.
Note that this approach modifies the input grid, which might not be desirable in all situations. If preserving the input is important, we should use one of the previous approaches.
We need to fill in the DP table of size m × n, and each cell takes constant time to compute.
We use a 2D DP array of size m × n to store the minimum path sum for each cell.
We still need to process each cell in the grid, which takes O(m * n) time.
We only use a 1D array of size n to store the minimum path sum for the current row.
We still need to process each cell in the grid, which takes O(m * n) time.
We modify the input grid in-place, so we don't need any additional space beyond the input.
123456789101112131415161718192021222324function minPathSum(grid): m = grid.length n = grid[0].length // Initialize DP array dp = new 2D array of size m × n // Set base case for top-left cell dp[0][0] = grid[0][0] // Set base cases for first row for j from 1 to n-1: dp[0][j] = grid[0][j] + dp[0][j-1] // Set base cases for first column for i from 1 to m-1: dp[i][0] = grid[i][0] + dp[i-1][0] // Fill DP array for i from 1 to m-1: for j from 1 to n-1: dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]) return dp[m-1][n-1]
12345678910111213141516171819202122function minPathSum(grid): m = grid.length n = grid[0].length // Initialize DP array for first row dp = new 1D array of size n dp[0] = grid[0][0] // Set base cases for first row for j from 1 to n-1: dp[j] = grid[0][j] + dp[j-1] // Fill DP array for i from 1 to m-1: // Update first column dp[0] = dp[0] + grid[i][0] // Update rest of the row for j from 1 to n-1: dp[j] = grid[i][j] + min(dp[j], dp[j-1]) return dp[n-1]
123456789101112131415161718function minPathSum(grid): m = grid.length n = grid[0].length // Set base cases for first row for j from 1 to n-1: grid[0][j] = grid[0][j] + grid[0][j-1] // Set base cases for first column for i from 1 to m-1: grid[i][0] = grid[i][0] + grid[i-1][0] // Fill grid for i from 1 to m-1: for j from 1 to n-1: grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1]) return grid[m-1][n-1]
123456789101112131415161718function minPathSum(grid): m = grid.length n = grid[0].length // Set base cases for first row for j from 1 to n-1: grid[0][j] = grid[0][j] + grid[0][j-1] // Set base cases for first column for i from 1 to m-1: grid[i][0] = grid[i][0] + grid[i-1][0] // Fill grid for i from 1 to m-1: for j from 1 to n-1: grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1]) return grid[m-1][n-1]
There are 3 main approaches to solve this problem:
The key insight for solving this problem is to use dynamic programming to build up the solution from smaller subproblems.
Let's define dp[i][j]
as the minimum path sum to reach the cell at position (i, j)
from the top-left corner (0, 0)
.
Since we can only move right or down, to reach the cell (i, j)
, we must come from either the cell above it (i-1, j)
or the cell to its left (i, j-1)
. Therefore, the recurrence relation is:
dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])
For the base cases:
dp[0][0] = grid[0][0]
(the top-left cell)i = 0
), dp[0][j] = grid[0][j] + dp[0][j-1]
(we can only come from the left)j = 0
), dp[i][0] = grid[i][0] + dp[i-1][0]
(we can only come from above)The final answer is dp[m-1][n-1]
, which represents the minimum path sum to reach the bottom-right corner.
We need to fill in the DP table of size m × n, and each cell takes constant time to compute.
We use a 2D DP array of size m × n to store the minimum path sum for each cell.
We can optimize the space complexity of the dynamic programming approach by using a 1D array instead of a 2D array.
The key observation is that when we're calculating dp[i][j]
, we only need the values from the current row and the row above it. Specifically, we need dp[i-1][j]
and dp[i][j-1]
.
If we use a 1D array dp
where dp[j]
represents the minimum path sum to reach the cell at position (i, j)
, then:
dp[j]
represents the minimum path sum to reach the cell at position (i-1, j)
.dp[j-1]
, dp[j-1]
represents the minimum path sum to reach the cell at position (i, j-1)
.So, the recurrence relation becomes:
dp[j] = grid[i][j] + min(dp[j], dp[j-1])
We need to be careful with the first column (j = 0
), as there is no cell to its left. For the first column, we simply update dp[0] = dp[0] + grid[i][0]
.
The final answer is dp[n-1]
, which represents the minimum path sum to reach the bottom-right corner.
We still need to process each cell in the grid, which takes O(m * n) time.
We only use a 1D array of size n to store the minimum path sum for the current row.
We can further optimize the space complexity by modifying the input grid in-place to store the minimum path sum for each cell.
The recurrence relation remains the same:
grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1])
We need to be careful with the base cases:
i = 0
), grid[0][j] = grid[0][j] + grid[0][j-1]
for all j from 1 to n-1.j = 0
), grid[i][0] = grid[i][0] + grid[i-1][0]
for all i from 1 to m-1.The final answer is grid[m-1][n-1]
, which represents the minimum path sum to reach the bottom-right corner.
Note that this approach modifies the input grid, which might not be desirable in all situations. If preserving the input is important, we should use one of the previous approaches.
We still need to process each cell in the grid, which takes O(m * n) time.
We modify the input grid in-place, so we don't need any additional space beyond the input.
123456789101112131415161718192021222324function minPathSum(grid): m = grid.length n = grid[0].length // Initialize DP array dp = new 2D array of size m × n // Set base case for top-left cell dp[0][0] = grid[0][0] // Set base cases for first row for j from 1 to n-1: dp[0][j] = grid[0][j] + dp[0][j-1] // Set base cases for first column for i from 1 to m-1: dp[i][0] = grid[i][0] + dp[i-1][0] // Fill DP array for i from 1 to m-1: for j from 1 to n-1: dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]) return dp[m-1][n-1]
Understand different approaches to solve the minimum path sum problem and analyze their efficiency.
The key insight for solving this problem is to use dynamic programming to build up the solution from smaller subproblems.
Let's define dp[i][j]
as the minimum path sum to reach the cell at position (i, j)
from the top-left corner (0, 0)
.
Since we can only move right or down, to reach the cell (i, j)
, we must come from either the cell above it (i-1, j)
or the cell to its left (i, j-1)
. Therefore, the recurrence relation is:
dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])
For the base cases:
dp[0][0] = grid[0][0]
(the top-left cell)i = 0
), dp[0][j] = grid[0][j] + dp[0][j-1]
(we can only come from the left)j = 0
), dp[i][0] = grid[i][0] + dp[i-1][0]
(we can only come from above)The final answer is dp[m-1][n-1]
, which represents the minimum path sum to reach the bottom-right corner.
We can optimize the space complexity of the dynamic programming approach by using a 1D array instead of a 2D array.
The key observation is that when we're calculating dp[i][j]
, we only need the values from the current row and the row above it. Specifically, we need dp[i-1][j]
and dp[i][j-1]
.
If we use a 1D array dp
where dp[j]
represents the minimum path sum to reach the cell at position (i, j)
, then:
dp[j]
represents the minimum path sum to reach the cell at position (i-1, j)
.dp[j-1]
, dp[j-1]
represents the minimum path sum to reach the cell at position (i, j-1)
.So, the recurrence relation becomes:
dp[j] = grid[i][j] + min(dp[j], dp[j-1])
We need to be careful with the first column (j = 0
), as there is no cell to its left. For the first column, we simply update dp[0] = dp[0] + grid[i][0]
.
The final answer is dp[n-1]
, which represents the minimum path sum to reach the bottom-right corner.
We can further optimize the space complexity by modifying the input grid in-place to store the minimum path sum for each cell.
The recurrence relation remains the same:
grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1])
We need to be careful with the base cases:
i = 0
), grid[0][j] = grid[0][j] + grid[0][j-1]
for all j from 1 to n-1.j = 0
), grid[i][0] = grid[i][0] + grid[i-1][0]
for all i from 1 to m-1.The final answer is grid[m-1][n-1]
, which represents the minimum path sum to reach the bottom-right corner.
Note that this approach modifies the input grid, which might not be desirable in all situations. If preserving the input is important, we should use one of the previous approaches.
We need to fill in the DP table of size m × n, and each cell takes constant time to compute.
We use a 2D DP array of size m × n to store the minimum path sum for each cell.
We still need to process each cell in the grid, which takes O(m * n) time.
We only use a 1D array of size n to store the minimum path sum for the current row.
We still need to process each cell in the grid, which takes O(m * n) time.
We modify the input grid in-place, so we don't need any additional space beyond the input.
123456789101112131415161718192021222324function minPathSum(grid): m = grid.length n = grid[0].length // Initialize DP array dp = new 2D array of size m × n // Set base case for top-left cell dp[0][0] = grid[0][0] // Set base cases for first row for j from 1 to n-1: dp[0][j] = grid[0][j] + dp[0][j-1] // Set base cases for first column for i from 1 to m-1: dp[i][0] = grid[i][0] + dp[i-1][0] // Fill DP array for i from 1 to m-1: for j from 1 to n-1: dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]) return dp[m-1][n-1]
12345678910111213141516171819202122function minPathSum(grid): m = grid.length n = grid[0].length // Initialize DP array for first row dp = new 1D array of size n dp[0] = grid[0][0] // Set base cases for first row for j from 1 to n-1: dp[j] = grid[0][j] + dp[j-1] // Fill DP array for i from 1 to m-1: // Update first column dp[0] = dp[0] + grid[i][0] // Update rest of the row for j from 1 to n-1: dp[j] = grid[i][j] + min(dp[j], dp[j-1]) return dp[n-1]
123456789101112131415161718function minPathSum(grid): m = grid.length n = grid[0].length // Set base cases for first row for j from 1 to n-1: grid[0][j] = grid[0][j] + grid[0][j-1] // Set base cases for first column for i from 1 to m-1: grid[i][0] = grid[i][0] + grid[i-1][0] // Fill grid for i from 1 to m-1: for j from 1 to n-1: grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1]) return grid[m-1][n-1]
123456789101112131415161718function minPathSum(grid): m = grid.length n = grid[0].length // Set base cases for first row for j from 1 to n-1: grid[0][j] = grid[0][j] + grid[0][j-1] // Set base cases for first column for i from 1 to m-1: grid[i][0] = grid[i][0] + grid[i-1][0] // Fill grid for i from 1 to m-1: for j from 1 to n-1: grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1]) return grid[m-1][n-1]