1. Different paths
1. The dp array is a two-dimensional array. First of all, we need to know the meaning of dp[i][j]. Since we are looking for different paths, the array corresponds to the different paths from the beginning to the ij position.
2. Due to the relationship, in fact, the path of the current position is the sum of the left and the top, and the condition of dp is: dp[i][j]=dp[i-1][j]+dp[i][j-1] ;
3. When initializing, we need to pay attention that there is only one way to go in the first row and the first column, so the initialization of the first row and the first column are both 1
4. The final return result is in dp[m-1][n-1];
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m,vector<int>(n,0));
for(int i=0;i<n;i++)
dp[0][i]=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-1][j]+dp[i][j-1];
}
}
return dp[m-1][n-1];
}
};
2. Different paths II
The general idea is actually basically the same as the previous question. It's just that the position data of the dp array corresponding to the blocked place needs to be adjusted.
1. During initialization, if there is a blockage, the subsequent data will be 0, because it cannot go to the end
2. Due to the relationship, in fact, the path of the current position is the sum of the left and the top. If there is no blocking of dp, the condition is: dp[i][j]=dp[i-1][j]+dp[i][j -1]; If blocked, then dp[i][j]=0
3. The final return result is in dp[m-1][n-1];
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
vector<vector<int>> dp(obstacleGrid.size(),vector<int>(obstacleGrid[0].size(),0));
for(int i=0;i<obstacleGrid.size();i++)
{
if(obstacleGrid[i][0]==1)
break;
dp[i][0]=1;
}
for(int i=0;i<obstacleGrid[0].size();i++)
{
if(obstacleGrid[0][i]==1)
break;
dp[0][i]=1;
}
for(int i=1;i<obstacleGrid.size();i++)
{
for(int j=1;j<obstacleGrid[0].size();j++)
{
if(obstacleGrid[i][j]==1)
dp[i][j]=0;
else
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
return dp[obstacleGrid.size()-1][obstacleGrid[0].size()-1];
}
};
3. Minimum path sum
The idea of this question is actually similar to the above
1. The dp array is a two-dimensional array, and the dp array represents the minimum path from the beginning to the current position
2. At the time of initialization, there is only one way to go in the first row and the first column, and the current position data of dp is the sum of the data of the last position of dp plus the current position of the grid array
3. Due to the relationship, it can be known that the minimum sum of the path at the current location is the smaller one between the left and the top plus the size of the path at the current location. The condition is dp[i][j]=grid[i][j]+ min(dp[i-1][j],dp[i][j-1]);
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
vector<vector<int>> dp(grid.size(),vector<int>(grid[0].size(),0));
dp[0][0]=grid[0][0];
for(int i=1;i<grid.size();i++)
dp[i][0]=grid[i][0]+dp[i-1][0];
for(int i=1;i<grid[0].size();i++)
dp[0][i]=grid[0][i]+dp[0][i-1];
for(int i=1;i<grid.size();i++)
{
for(int j=1;j<grid[0].size();j++)
dp[i][j]=grid[i][j]+min(dp[i-1][j],dp[i][j-1]);
}
return dp[grid.size()-1][grid[0].size()-1];
}
};