64 Minimum path sum (recursive, dynamic programming)

1. Problem description:

Given an m x n grid containing non-negative integers, 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.

Note: You can only move one step down or right at a time.

Example:

Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: the smallest sum because the path 1 → 3 → 1 → 1 → 1 in.

Source: LeetCode
link: https://leetcode-cn.com/problems/minimum-path-sum

2. Thinking analysis:

① First of all, we can use recursion to solve the problem about trying the path, which can be divided into two parallel states of walking down and walking right. At the beginning, we need to pass in a starting point to indicate the current position, and use if judgment to decide whether to Go to the right or down, recurse if you can, add a variable k in the recursive method to record the path and the current position, if you can go down, then the next point will be The value is added to the sum of the path, which means the sum of the next path can be reached, but the recursive time-consuming is relatively large, for the large amount of data, but only through 25 test cases

It is also worth learning in the recursive code provided by the official. It is recursion with a return value. When recursing, the value of the current position is added to the minimum value of down recursion and left recursion. The idea is worth learning.

② Because the recursion timed out, I still want to solve other problems. In fact, this problem is relatively simple to understand. It is easy to find that the minimum value of the current position in the current path in the shortest path and the formation process is determined by the left element and The minimum value of the upper element is added to the value of the current position. The combination matrix is ​​very easy to understand. As long as we keep doing this in the loop, we can get the minimum value of the current position.

③ You can also use a one-dimensional array to solve the problem of collar buckle, but it is more difficult to understand. If you use a two-dimensional array, it will be less difficult to understand

3. The code is as follows:

Recursive code I wrote myself:

import java.util.Scanner;
public class Solution {
    /*使用一个全局变量来记录最小值*/
    int min = Integer.MAX_VALUE;
    public int minPathSum(int[][] grid) {
        dfs(grid, grid[0][0], grid.length, grid[0].length, 0, 0);
        return min;
    }

    public void dfs(int[][] grid, int k, int row, int col, int r, int c) {
        if (r == row - 1 && c == col - 1){
            min = Math.min(min, k);
            return;
        }
        /*提前剪枝: 两个平行状态*/
        /*向右走*/
        if (c + 1 < col){
            dfs(grid, k + grid[r][c + 1], row, col, r, c + 1);
        }
        if (r + 1 < row){
            dfs(grid, k + grid[r + 1][c], row, col, r + 1, c);
        }
    }
}

The official has a recursive return value:

public class Solution {
    public int calculate(int[][] grid, int i, int j) {
        if (i == grid.length || j == grid[0].length) return Integer.MAX_VALUE;
        if (i == grid.length - 1 && j == grid[0].length - 1) return grid[i][j];
        return grid[i][j] + Math.min(calculate(grid, i + 1, j), calculate(grid, i, j + 1));
    }
    public int minPathSum(int[][] grid) {
        return calculate(grid, 0, 0);
    }
}

My own dynamic programming code:

import java.util.Scanner;
public class Solution {
    public int minPathSum(int[][] grid) {
        int r = grid.length, c = grid[0].length;
        int dp[][] = new int[r][c];
        dp[0][0] = grid[0][0];
        /*初始化第一行*/
        for (int i = 1; i < c; ++i){
            dp[0][i] = dp[0][i - 1] + grid[0][i];
        }
        /*初始化第一列*/
        for (int i = 1; i < r; ++i){
            dp[i][0] = dp[i - 1][0] + grid[i][0];
        }
        for (int i = 1; i < r; ++i){
            for (int j = 1; j < c; ++j){
                /*逻辑其实很好理解到达当前最短的路径和: 左边元素与上边元素的最小值*/
                dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
            }
        }
        return dp[r - 1][c - 1];
    }
}

 

Published 569 original articles · Like 153 · Visits 590,000+

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/105235312