The sword refers to offer98: the number of paths

Question:
A robot is located in the upper left corner of an mxn grid (the starting point is marked "Start" in the image below).
The robot can only move down or to the right one step at a time. The robot tries to reach the bottom right corner of the grid (labeled "Finish" in the image below).
Q How many different paths are there in total?
insert image description here
Example 1:
Input: m = 3, n = 2
Output: 3
Explanation:
Starting from the upper left corner, there are a total of 3 paths to the lower right corner.

  1. right -> down -> down
  2. down -> down -> right
  3. Down -> Right -> Down
    Example 2:
    Input: m = 7, n = 3
    Output: 28
    Analysis:
    First of all, to determine the state transition equation, you can use the function f(i,j) to indicate that from the upper left corner of the grid The number of paths from the position with coordinates (0,0) to the position with coordinates (i,j). Since the robot can only move down or to the right, when both i and j are greater than 0, the robot has two ways to reach the position with the coordinates (i, j). ) can also go one step down from the position with coordinates (i, j-1), so f(i, j)=f(i-1, j)+f(i, j-1 ).
    If the two-dimensional array is regarded as a table, the first column and the first row are initialized to 1, and then calculated row by row through the state transition equation.
    insert image description here
    To further optimize the space efficiency, you only need to create a one-dimensional array dp, that is, dp[n].
    See the figure and code below for details.
    insert image description here
    Code:
import java.util.Arrays;

public class UniquePaths {
    
    
//    时间空间复杂度都是mn
    public int uniquePaths1(int m, int n) {
    
    
        int[][] dp = new int[m][n];
        return helper(m - 1, n - 1, dp);
    }
    private int helper(int i, int j, int[][] dp) {
    
    
        if (dp[i][j] == 0) {
    
    
//            由于机器人只能向下或者向右走,因此当i=0时,机器人位于最上面的一行,机器人不可能
//            从某个位置向下走一步到达i等于0的位置,j=0时同理。
            if (i == 0 || j == 0){
    
    
                dp[i][j] = 1;
            }else {
    
    
                dp[i][j] = helper(i-1,j,dp)+helper(i,j-1,dp);
            }
        }
        return dp[i][j];
    }
//    时间复杂度同样是mn
    public int uniquePaths2(int m, int n) {
    
    
        int[][] dp = new int[m][n];
        Arrays.fill(dp[0],1);
        for (int i = 0; i < m; i++) {
    
    
            dp[i][0] = 1;
        }
        for (int i = 1; i < m; i++) {
    
    
            for (int j = 1; j < n; j++) {
    
    
                dp[i][j] = dp[i][j-1]+dp[i-1][j];
            }
        }
        return dp[m-1][n-1];
    }
//    优化空间效率,空间效率优化成了O(ns)
    public int uniquePaths3(int m, int n){
    
    
        int[] dp = new int[n];
        Arrays.fill(dp,1);
        for (int i = 1; i < m; i++) {
    
    
            for (int j = 1; j < n; j++) {
    
    
                dp[j] +=dp[j-1];
            }
        }
        return dp[n-1];
    }
}

insert image description here

Guess you like

Origin blog.csdn.net/Jiaodaqiaobiluo/article/details/123174968