LeetCode [62] Different Path (DP)

This question is relatively common in interviews. It mainly investigates the understanding of the algorithm. The optimal solution is dynamic programming. This article writes about the dynamic programming ideas and solutions of this question.

Title description

Different paths
A robot is located in the upper left corner of an mxn grid (the starting point is "Start" in the figure), and the robot can only move one step down or right at a time. The robot tries to reach the lower right corner of the grid (marked "Finish" in the figure). How many different paths are there in total?

Example one:

Input: m = 3, n = 2
Output: 3
Explanation: Starting from the upper left corner, there are a total of 3 paths that can reach the lower right corner.

  1. Right-> right-> down
  2. Right-> down-> right
  3. Down-> right-> right

Example two:

Input: m = 7, n = 3
Output: 28

prompt:

  • 1 <= m, n <= 100
  • Question data to ensure that the answer is less than or equal to 2 * 10 ^ 9 (int type)

Solution ideas

  1. Brute force method: use DFS traversal, from (1, 1)-> (m, n), when the bottom right corner is reached, the count is increased by one until the end of the traversal. Can only solve part of the use cases, will perform timeout, time complexity O (N ^ 2)
  2. Dynamic planning: open up a new state space and save the state value of each point, the state value represents the number of paths that can be reached to reach the point. When finally reaching the lower right corner, the number of paths recorded at this point is the total number of different paths. Because the state value of the two-dimensional array only needs to be traversed once, the time complexity is O (N)

How to find the state value of each point? This problem uses mathematical induction. Observation shows that
when m = 1, n = 1, the starting point is reached without going to the right and down, and the number of paths is 1;
when m = 1, n = 2, starting pointOnly downOne step to reach the target point, the number of ways is 1;
when m = 2, n = 1, the starting pointRight onlyOne step to reach the target point, the number of ways is 1;
when m = 2, n = 2, the starting point can beOne step to the right and then to the next,orFirst down then rightTo reach the target point, the number of ways is 2;
when m = 2, n = 3, the starting point can be two steps down and then one step to the right, or the point (2, 2) and then the next step, and the point (2, 2 ) Is 2, so the total number of ways to reach the target point is 3;
and so on, you can find the law, the number of ways to reach the point (m, n) is (m-1, n) the number of ways plus (m , n-1) The sum of the number of ways, namely:status[m][n] = status[m - 1][n] + status[m][n-1](State transfer equation)

Code

Recursive code
int status[101][101] = {0};
int uniquePaths(int m, int n)
{
    if(m <= 0 || n <= 0) {
        return 0;
    }
    if(m == 1 || n == 1) {
        return 1;
    }
    // 当该点状态值 > 0 时,说明已经计算过路径数,无需重复计算
    if(status[m][n] > 0) {
        return status[m][n];
    }
    status[m - 1][n] = uniquePaths(m - 1, n);
    status[m][n - 1] = uniquePaths(m, n - 1);
    status[m][n] = status[m - 1][n] + status[m][n - 1];
    return status[m][n];
}
Iteration code
int uniquePaths(int m, int n)
{
    int status[101][101] = {0};
    int i, j;
    for(i = 1; i <= m; i++) {
        for(j = 1; j <= n; j++) {
            if(i == 1 || j == 1) {
                status[i][j] = 1;
            }
            else {
                status[i][j] = status[i - 1][j] + status[i][j -1];
            }
        }
    }
    return status[m][n];
}

Different paths

Published 53 original articles · Like 89 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/xiaoma_2018/article/details/105499393