Minimum path sum
Title description: Given a
m x n
gridgrid
containing non-negative integers, please find a path from the upper left corner to the lower right corner so that the sum of the numbers on the path is the smallest.description: can only move one step down or right at a time.
Please refer to LeetCode official website for example description.
Source: LeetCode
Link: https://leetcode-cn.com/problems/minimum-path-sum/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution 1: Exhaustive recursion
The parameters of the recursive method
minPathSum
are the current coordinates and the current path sum. The recursive process is as follows:
- If the current coordinate has reached the lower right corner, judge whether the current path sum is smaller than the existing minimum value, and if it is, update the minimum value;
- If the current coordinate goes to the far right, the coordinate will move down, recursive processing;
- If the current coordinate is at the bottom, the coordinate will move to the right, and the process will be processed recursively;
- If the current coordinate is not on the edge, you need to recurse twice to move down and right.
Finally, the minimum value is returned.
Solution two: dynamic programming
- First, declare a two-dimensional array dp same size as the grid to store the accumulated value to the corresponding cell;
- Initialize the value of the first column;
- Initialize the value of the first row;
- Then traverse the cells behind and assign the smaller value on the left and above;
- Finally,
dp[rows - 1][columns - 1]
returned as the smallest sum.
public class LeetCode_064 {
private static int result = Integer.MAX_VALUE;
/**
* 穷举法 递归
*
* @param grid
* @return
*/
public static int minPathSum(int[][] grid) {
minPathSum(grid, 0, 0, 0);
return result;
}
private static void minPathSum(int[][] grid, int x, int y, int sum) {
sum += grid[x][y];
// 走到右下角的单元格
if (x == grid.length - 1 && y == grid[0].length - 1) {
result = Math.min(sum, result);
return;
}
if (x == grid.length - 1) {
// 走到最后一行,往右走
minPathSum(grid, x, y + 1, sum);
} else if (y == grid[0].length - 1) {
// 走到最后一列,往下走
minPathSum(grid, x + 1, y, sum);
} else {
// 往下走
minPathSum(grid, x, y + 1, sum);
// 往右走
minPathSum(grid, x + 1, y, sum);
}
}
/**
* 动态规划
*
* @param grid
* @return
*/
public static int minPathSum2(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int rows = grid.length, columns = grid[0].length;
int[][] dp = new int[rows][columns];
dp[0][0] = grid[0][0];
/**
* 初始化第一列的值
*/
for (int i = 1; i < rows; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
/**
* 初始化第一行的值
*/
for (int j = 1; j < columns; j++) {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
/**
* 当前的单元格取较小值
*/
for (int i = 1; i < rows; i++) {
for (int j = 1; j < columns; j++) {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
}
}
return dp[rows - 1][columns - 1];
}
public static void main(String[] args) {
int[][] grid = new int[][]{{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
System.out.println(minPathSum(grid));
System.out.println(minPathSum2(grid));
}
}
[Daily Message] What the poor lack: superficial lack of funds, lack of ambition in nature, lack of ideas in mind, lack of understanding of opportunities, lack of courage, lack of action for change, lack of perseverance in career.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。