Below is the implementation of the minimum path sum:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122/** * Find the minimum path sum from top-left to bottom-right. * Dynamic Programming (2D) approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSum(grid) { const m = grid.length; const n = grid[0].length; // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base case for top-left cell dp[0][0] = grid[0][0]; // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = grid[0][j] + dp[0][j - 1]; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = grid[i][0] + dp[i - 1][0]; } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1];} /** * Find the minimum path sum from top-left to bottom-right. * Dynamic Programming (1D) approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSumOptimized(grid) { const m = grid.length; const n = grid[0].length; // Initialize DP array for first row const dp = Array(n).fill(0); dp[0] = grid[0][0]; // Set base cases for first row for (let j = 1; j < n; j++) { dp[j] = grid[0][j] + dp[j - 1]; } // Fill DP array for (let i = 1; i < m; i++) { // Update first column dp[0] = dp[0] + grid[i][0]; // Update rest of the row for (let j = 1; j < n; j++) { dp[j] = grid[i][j] + Math.min(dp[j], dp[j - 1]); } } return dp[n - 1];} /** * Find the minimum path sum from top-left to bottom-right. * In-place Dynamic Programming approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSumInPlace(grid) { const m = grid.length; const n = grid[0].length; // Create a copy of the grid to avoid modifying the input const dp = grid.map(row => [...row]); // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = dp[0][j] + dp[0][j - 1]; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = dp[i][0] + dp[i - 1][0]; } // Fill grid for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = dp[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1];} // Test casesconst grid1 = [ [1, 3, 1], [1, 5, 1], [4, 2, 1]];console.log(minPathSum(grid1)); // 7console.log(minPathSumOptimized(grid1)); // 7console.log(minPathSumInPlace(grid1)); // 7 const grid2 = [ [1, 2, 3], [4, 5, 6]];console.log(minPathSum(grid2)); // 12console.log(minPathSumOptimized(grid2)); // 12console.log(minPathSumInPlace(grid2)); // 12
Let's break down the implementation:
Implement the minimum path sum solution in different programming languages.
Below is the implementation of the minimum path sum in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122/** * Find the minimum path sum from top-left to bottom-right. * Dynamic Programming (2D) approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSum(grid) { const m = grid.length; const n = grid[0].length; // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base case for top-left cell dp[0][0] = grid[0][0]; // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = grid[0][j] + dp[0][j - 1]; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = grid[i][0] + dp[i - 1][0]; } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1];} /** * Find the minimum path sum from top-left to bottom-right. * Dynamic Programming (1D) approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSumOptimized(grid) { const m = grid.length; const n = grid[0].length; // Initialize DP array for first row const dp = Array(n).fill(0); dp[0] = grid[0][0]; // Set base cases for first row for (let j = 1; j < n; j++) { dp[j] = grid[0][j] + dp[j - 1]; } // Fill DP array for (let i = 1; i < m; i++) { // Update first column dp[0] = dp[0] + grid[i][0]; // Update rest of the row for (let j = 1; j < n; j++) { dp[j] = grid[i][j] + Math.min(dp[j], dp[j - 1]); } } return dp[n - 1];} /** * Find the minimum path sum from top-left to bottom-right. * In-place Dynamic Programming approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSumInPlace(grid) { const m = grid.length; const n = grid[0].length; // Create a copy of the grid to avoid modifying the input const dp = grid.map(row => [...row]); // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = dp[0][j] + dp[0][j - 1]; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = dp[i][0] + dp[i - 1][0]; } // Fill grid for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = dp[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1];} // Test casesconst grid1 = [ [1, 3, 1], [1, 5, 1], [4, 2, 1]];console.log(minPathSum(grid1)); // 7console.log(minPathSumOptimized(grid1)); // 7console.log(minPathSumInPlace(grid1)); // 7 const grid2 = [ [1, 2, 3], [4, 5, 6]];console.log(minPathSum(grid2)); // 12console.log(minPathSumOptimized(grid2)); // 12console.log(minPathSumInPlace(grid2)); // 12
Set up a dynamic programming array to keep track of the minimum path sum to reach each cell in the grid.
Define the base cases for the top-left cell, the first row, and the first column.
For each cell, calculate the minimum path sum by adding the value of the cell to the minimum of the path sums from the cell above and the cell to the left.
Return the value in the bottom-right corner of the DP array, which represents the minimum path sum to reach the destination.
Reduce the space complexity by using a 1D array instead of a 2D array, or by modifying the input grid in-place.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the minimum path sum problem using a different approach than shown above.
Handle the case where the grid has only one cell.
Handle the case where the grid has only one row.
Handle the case where the grid has only one column.
Handle large grid sizes efficiently to avoid timeout issues.
Below is the implementation of the minimum path sum:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122/** * Find the minimum path sum from top-left to bottom-right. * Dynamic Programming (2D) approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSum(grid) { const m = grid.length; const n = grid[0].length; // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base case for top-left cell dp[0][0] = grid[0][0]; // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = grid[0][j] + dp[0][j - 1]; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = grid[i][0] + dp[i - 1][0]; } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1];} /** * Find the minimum path sum from top-left to bottom-right. * Dynamic Programming (1D) approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSumOptimized(grid) { const m = grid.length; const n = grid[0].length; // Initialize DP array for first row const dp = Array(n).fill(0); dp[0] = grid[0][0]; // Set base cases for first row for (let j = 1; j < n; j++) { dp[j] = grid[0][j] + dp[j - 1]; } // Fill DP array for (let i = 1; i < m; i++) { // Update first column dp[0] = dp[0] + grid[i][0]; // Update rest of the row for (let j = 1; j < n; j++) { dp[j] = grid[i][j] + Math.min(dp[j], dp[j - 1]); } } return dp[n - 1];} /** * Find the minimum path sum from top-left to bottom-right. * In-place Dynamic Programming approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSumInPlace(grid) { const m = grid.length; const n = grid[0].length; // Create a copy of the grid to avoid modifying the input const dp = grid.map(row => [...row]); // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = dp[0][j] + dp[0][j - 1]; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = dp[i][0] + dp[i - 1][0]; } // Fill grid for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = dp[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1];} // Test casesconst grid1 = [ [1, 3, 1], [1, 5, 1], [4, 2, 1]];console.log(minPathSum(grid1)); // 7console.log(minPathSumOptimized(grid1)); // 7console.log(minPathSumInPlace(grid1)); // 7 const grid2 = [ [1, 2, 3], [4, 5, 6]];console.log(minPathSum(grid2)); // 12console.log(minPathSumOptimized(grid2)); // 12console.log(minPathSumInPlace(grid2)); // 12
Let's break down the implementation:
Implement the minimum path sum solution in different programming languages.
Below is the implementation of the minimum path sum in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122/** * Find the minimum path sum from top-left to bottom-right. * Dynamic Programming (2D) approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSum(grid) { const m = grid.length; const n = grid[0].length; // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base case for top-left cell dp[0][0] = grid[0][0]; // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = grid[0][j] + dp[0][j - 1]; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = grid[i][0] + dp[i - 1][0]; } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1];} /** * Find the minimum path sum from top-left to bottom-right. * Dynamic Programming (1D) approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSumOptimized(grid) { const m = grid.length; const n = grid[0].length; // Initialize DP array for first row const dp = Array(n).fill(0); dp[0] = grid[0][0]; // Set base cases for first row for (let j = 1; j < n; j++) { dp[j] = grid[0][j] + dp[j - 1]; } // Fill DP array for (let i = 1; i < m; i++) { // Update first column dp[0] = dp[0] + grid[i][0]; // Update rest of the row for (let j = 1; j < n; j++) { dp[j] = grid[i][j] + Math.min(dp[j], dp[j - 1]); } } return dp[n - 1];} /** * Find the minimum path sum from top-left to bottom-right. * In-place Dynamic Programming approach. * * @param {number[][]} grid - Grid filled with non-negative integers * @return {number} - Minimum path sum */function minPathSumInPlace(grid) { const m = grid.length; const n = grid[0].length; // Create a copy of the grid to avoid modifying the input const dp = grid.map(row => [...row]); // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = dp[0][j] + dp[0][j - 1]; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = dp[i][0] + dp[i - 1][0]; } // Fill grid for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = dp[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1];} // Test casesconst grid1 = [ [1, 3, 1], [1, 5, 1], [4, 2, 1]];console.log(minPathSum(grid1)); // 7console.log(minPathSumOptimized(grid1)); // 7console.log(minPathSumInPlace(grid1)); // 7 const grid2 = [ [1, 2, 3], [4, 5, 6]];console.log(minPathSum(grid2)); // 12console.log(minPathSumOptimized(grid2)); // 12console.log(minPathSumInPlace(grid2)); // 12
Set up a dynamic programming array to keep track of the minimum path sum to reach each cell in the grid.
Define the base cases for the top-left cell, the first row, and the first column.
For each cell, calculate the minimum path sum by adding the value of the cell to the minimum of the path sums from the cell above and the cell to the left.
Return the value in the bottom-right corner of the DP array, which represents the minimum path sum to reach the destination.
Reduce the space complexity by using a 1D array instead of a 2D array, or by modifying the input grid in-place.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the minimum path sum problem using a different approach than shown above.
Handle the case where the grid has only one cell.
Handle the case where the grid has only one row.
Handle the case where the grid has only one column.
Handle large grid sizes efficiently to avoid timeout issues.