LeetCode 62, 63 Unique Paths I+II

Line space may be optimized by O (n), for the column can be optimized by O (m).

class Solution {
public:
    int uniquePaths(int m, int n) {
        int dp[m][n]={0};
        dp[0][0]=1;
        for (int i=0;i<m;++i){
            for (int j=0;j<n;++j){
                if (i==0&&j==0) continue;
                dp[i][j]=0;
                if (j-1>=0) dp[i][j] += dp[i][j-1];
                if (i-1>=0) dp[i][j] += dp[i-1][j];
            }
        }           
        return dp[m-1][n-1];
    }
};

And the previous question, like, pay attention to places like the obstacle to zero.

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m=obstacleGrid.size(), n=obstacleGrid[0].size();
        vector<vector<int>> dp(m,vector<int>(n,0));
        dp[0][0] = obstacleGrid[0][0]==0?1:0;
        for (int i=0;i<m;++i){
            for (int j=0;j<n;++j){
                if (i==0 && j==0) continue;
                else if (obstacleGrid[i][j]==1) dp[i][j]=0;
                else if (j==0) dp[i][j]=dp[i-1][j];
                else if (i==0) dp[i][j]=dp[i][j-1];
                else dp[i][j] = dp[i-1][j]+dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }
};

 

Unique Path variant - Google HF

Went to the upper right corner from the top left corner, every step requires only go to the positive right upper or lower right, namely → ↗↘

In columns dp, dp [i] [j] = dp [i-1] [j-1] + dp [i] [j-1] + dp [i + 1] [j-1], Note i-1 , i + 1 requires the presence of

 

followup1:  Optimization space complexity to O (n)

You can dp [m] [2] Scroll array do. If it is a one-dimensional dp [m], then, a need to record the value of the variable prev upper left.

 

followup2: Given three points in the rectangle, it determines whether there is a path to traverse these three points

Paint will be able to discover the laws observed. Because each cell can only go from three directions, consider two lattice (x, y) and (i, j), when (x, y) can come to (i, j) when, (x, y sector area) will be located at (i, j) back sheets inside

Is expressed, i with the formula - dy <= x <= i + dy, where dy = j - y

After sorting (Here we can think about how to sort) is not satisfied if they find a group does not exist

 

followup3: Given three points in the rectangle to find all the number of paths traverse these three points

Or use the scroll array dp along the lines of follow up 1, but if there is a current need to get to the point column, the only point dp, other grid all set to zero, indicating that we only care path through the target point on this column

(If a plurality of the points to be achieved, the direct return 0)

 

followup4: Given a lower bound (i == H), find a number of paths through all to set boundaries (i> = H)

1: First dp again, get all amount to the upper right path

2: then 0 <= i <= H, 0 <= j <= cols small rectangle again and then get all the DP paths without the lower bound of the number of

3: Results obtained by subtracting the number of two final path

The code can be reused follow up of 1

public int uniquePaths(int rows, int cols, int H) {
    return uniquePaths(rows, cols) - uniquePaths(H, cols);
}

 

followup5: start and end changed from top left to bottom left, each step can only ↓ ↘↙, find the number of all possible paths

Dp to row

 

Reference

https://www.1point3acres.com/bbs/thread-423857-1-1.html

Guess you like

Origin www.cnblogs.com/hankunyan/p/11479116.html