[Dynamic programming] Find the path


Solve a relatively simple and easy-to-understand problem in dynamic programming today, and find the path

Bull link

Question: A robot is in the upper left corner (starting point) of a map of size m×n.

The robot can move down or right each time. The robot has to reach the lower right corner (end point) of the map.

How many different paths can there be from the start point to the end point?

img

Remarks: m and n are less than or equal to 100, and the calculation result is guaranteed to be within the range of int

Data range: 0<n,m<=100, ensure that the calculation result is within the range of 32-bit integers

conventional solution

analysis of idea:

The path from the starting point to the ending point can actually be decomposed into the path from the starting point to the coordinates (i, j). According to the meaning of the question, it can only move down or to the right. Therefore, from the starting point to the coordinate (0, j) is the first The row has only one path to the right, from the starting point to the coordinate (i, 0), that is, the first column has only one path down, and the rest of the coordinates (i, j) can be walked down by the coordinates (i-1, j) One step can also be reached by one step to the right by the coordinates (i, j-1), and the value of the final end coordinate (m-1, n-1) is all paths from the starting point to the end point.

insert image description here

In summary, the four angles of the state:

  1. State definition F(i, j): the number of paths from (0, 0) to coordinates (i, j)
  2. The transition equation between states defines F(i,j) = F(i-1,j) + F(i,j-1)
  3. Initialization of state i == 0, F(0, j) = 1; j == 0, F(i, 0) = 1
  4. Return the result F(m-1, n-1)
import java.util.*;
public class Solution {
    
    
    public int uniquePaths (int m, int n) {
    
    
        //定义count数组存放每个坐标的状态
        int[][] count = new int[m][n];
        //第0列的坐标的状态都为1
        for(int i = 0;i < m;i ++){
    
    
            count[i][0] = 1;
        }
        //第0行的坐标的状态都为1
        for(int j = 1;j < n;j ++){
    
    
            count[0][j] = 1;
        }
        //其余坐标的状态
        for(int i = 1;i < m;i ++){
    
    
            for(int j = 1;j < n;j ++){
    
    
                count[i][j] = count[i-1][j] + count[i][j-1];//转移状态
            }
        }
        return count[m-1][n-1];//返回状态数组的最后一个元素即终点坐标状态值
    }
}

Time Complexity: O(mn)

Space Complexity: O(mn)

Mathematical solution

Solving by mathematical formula will increase the time and space complexity, but it also has the disadvantage that it is not easy to think of at the first time.

In this question, if you think about it in a holistic way, from the starting point to the end point, since you can only go down and to the right, it means that you will definitely take m-1 steps down and n-1 steps to the right. Only in this way can we reach the end point, which means that we only need to arrange and combine the m-1 steps down and n-1 steps to the right, and we take m+n-2 steps in total.

Find the combination formulainsert image description here
insert image description here

import java.util.*;
public class Solution {
    
    
    public int uniquePaths (int m, int n) {
    
    
        long ret = 1L;
        for (int x = n, y = 1; y < m; ++x, ++y) {
    
    
        //组合公式的循环求解
            ret = ret * x / y;
        }
        return (int)ret;
    }
}

Time complexity: O(min(n,m))

Space Complexity: O(1)

Compared with the above dynamic programming method, the space complexity of this method is low, but if the data is too large, it may overflow and it is not easy to think about it
!

Guess you like

Origin blog.csdn.net/weixin_46103589/article/details/121942665